Newbee21369 has asked for the wisdom of the Perl Monks concerning the following question:

<ü>I'm trying to read only files in a directory. I have tried the code below but still having issues. Any suggestions?

#### Need to keep this code unchanged opendir DIR, "Whatever/path"; @files=grep { !/^\.+$/ } readdir(DIR); closedir DIR; ####Need to return only files in code shown below. I have tried -f $fi +le, !-d $file, -e$file in my if condition and it is still returning F +iles and directories. foreach $file (@files) { if (-f $file) { #print whatever } }

20060801 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Read only Files in a Directory
by Mr. Muskrat (Canon) on Aug 01, 2006 at 20:26 UTC
    Your file test does not include the complete path to the files so perl is looking in the current working directory for them.
Re: Read only Files in a Directory
by swampyankee (Parson) on Aug 01, 2006 at 21:21 UTC

    I've used something akin to:

    $base = '/usr/local/bin/'; @glob = grep { -f ($base . $_)} @glob;

    to filter out everything which isn't a file (I did it on Windows, but -f is portable). glob is quite handy for this, although characters meaningful to csh must be escaped.


    corrected really dumb spelling error.

    emc

    Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.

    Groucho Marx
      characters meaningful to csh must be escaped

      Not in my experience (on a Unix, though):

      ## create some files with funny names map { open my $fd, '>', chr $_ or die "open: $!"; close $fd; } 33..45,58..63,91..96,123..126 $ perl -le 'print glob q[+]' + $ perl -le 'print glob q[`]' ` $ perl -le 'print glob q[;]' ;

      The only gotchas are * and ?:

      $ perl -le 'print glob q[?]' !"#$%&'()*+,-:;<=>?[\]^_`{|}~ $ perl -le 'print glob q[\?]' ? $ perl -le 'print glob q[*]' !"#$%&'()*+,-:;<=>?[\]^_`{|}~ $ perl -le 'print glob q[\*]' *

      --
      David Serrano

        Not in my experience (on a Unix, though):

        It is my experience on Windows: it gets confused about blanks in directory names, possibly because it's implemented differently in the Windows port than in the various *ixen.

        I don't know whether using opendir and readdir is better than glob, but I usually find glob more convenient.

        emc

        Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.

        Groucho Marx
Re: Read only Files in a Directory
by jwkrahn (Abbot) on Aug 01, 2006 at 21:54 UTC
    #### Need to keep this code unchanged opendir DIR, "Whatever/path"; @files=grep { !/^\.+$/ } readdir(DIR); closedir DIR;
    Your comment is wrong, you definitely need to change that code.
    #### Need to keep this code unchanged opendir DIR, 'Whatever/path' or die "Cannot open 'Whatever/path' $!"; my @files = map "Whatever/path/$_" grep !/\A\.\.?\z/, readdir DIR; closedir DIR;
      my @files = map "Whatever/path/$_" grep !/\A\.\.?\z/, readdir DIR;
      I might be wrong but don't you need a comma in map EXPR, list. If so, that line should be

      my @files = map "Whatever/path/$_", grep !/\A\.\.?\z/, readdir DIR;

      Cheers,

      JohnGG

        Oops, thanks for fixing that.   :-)
        It worked for me too. Thanks. Just changed the "Whatever/path/$_" as "$_" Thanks
Re: Read only Files in a Directory
by zentara (Cardinal) on Aug 01, 2006 at 20:31 UTC
    Your program is working for me on linux.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      After reading Mr. Muskrat's fine explanation, mine works when
      opendir DIR, ".";

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum