Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Directory Listing to an Array

by Poetic Justice (Monk)
on Apr 10, 2002 at 22:55 UTC ( [id://158156]=CUFP: print w/replies, xml ) Need Help??

Works with Win32, haven't tested it on *nix yet. Feedback and/or criticism welcome.
sub listfiles { $path = shift; $path = "." unless $path; opendir( DIR, $path ) || die "Can't open $path: $!"; !/^\./ and push @entry, "$_\n" while ($_ = readdir(DIR)); #removes + the . and .. entries from readdir closedir( DIR ); return @entry; }

Replies are listed 'Best First'.
Re: Directory Listing to an Array
by japhy (Canon) on Apr 10, 2002 at 23:12 UTC
    I'd scope the variables and what-not; also, you're doing far more work than necessary.
    sub ls { my $path = @_ ? shift : "."; local *DIR; opendir DIR, $path or die "can't ls $path: $!"; return grep { $_ ne "." and $_ ne ".." } readdir DIR; }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      what does the shift : "." do?
        The ?: operator is like a condensed if/else block.
        my $path; # if an argument was passed, use it if (@_) { $path = shift } # otherwise, use "." else { $path = "." }
        That can be written as my $path = @_ ? shift : "."; -- and if I wanted to be even shiftier (pun intended), I could do my ($path) = (@_, "."); but that's probably rude.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Directory Listing to an Array
by emilford (Friar) on Apr 11, 2002 at 04:18 UTC
    I always just do....
    opendir(DIR, "$path") || die "Error: $!"; @entry = grep { $_ ne '.' && $_ ne '..' } readdir(DIR); closedir(DIR);
    This works on *NIX and I believe WIN32 as well, but I'm not 100% certain. -Eric
Re: Directory Listing to an Array
by ehdonhon (Curate) on Apr 10, 2002 at 23:28 UTC

    Your routine is named "listfiles", but it does more than just list files (it also lists directories, FIFO pipes, sockets, etc...) Also, it appears that you will skip any dot-files ( such as '.cshrc' ), not just '.' and '..'. Depending on what you are using this for, that might not be optimal.

    sub listfiles { my $path = shift || '.'; opendir ( DIR, $path ) or die "Can't open path: $!\n"; return grep { -f "$path/$_" } readdir ( DIR ); }
      (minor bug) this will fail on a directory named 0. either use japhy's solution above (which checks the length of @_,) or check $_[0] for definedness, not truth.

      ~Particle ;Þ

        Ohh, yeah. Good point. It will be nice in perl 6 when we can say

        my $dir = shift // '.';

        :-)

Re: Directory Listing to an Array
by vkonovalov (Monk) on Apr 29, 2002 at 06:12 UTC
    This is *dangerous* code snippet, because it corrupts value of $_ variable by while($_=readdir).
    (didn't you read "seven sins of perl (revisited)?")
    You should place local $_ somewhere at the beginning of a sub. Or write instead of "while" ..... for (readdir) which will localize $_ itself.

      $map = 'c:\\some\\directory'

      opendir (Directory, $map) or die "Cannot Open Directory";

      @contents = readdir(Directory);

      closedir(Directory);

      splice(@contents, 0, 2);  # removes " . & .. "

      foreach $content (@contents) {

          print "$content\n";

      }

      I have used this to capture the contents of a directory... Darrick...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://158156]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (None)
    As of 2024-04-25 00:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found