in reply to Directory Listing to an Array

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 ); }

Replies are listed 'Best First'.
Re: Re: Directory Listing to an Array
by particle (Vicar) on Apr 11, 2002 at 01:26 UTC
    (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 // '.';

      :-)