Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Perl List files in directory

by bigj (Monk)
on Apr 22, 2014 at 11:06 UTC ( [id://1083137]=note: print w/replies, xml ) Need Help??


in reply to Perl List files in directory

In addition to the others, you might also try not to reinvent the wheel. Whenever you need to search files, the 1st recommandation should be File::Find. In your case
use File::Find; find({wanted => sub {-f and print "file = $_\n"},follow => 1},'.');
already does what you intend to do in a shorter, easier readable, more maintainable and much less errorprone way.

Greetings,
Janek Schleicher

Replies are listed 'Best First'.
Re^2: Perl List files in directory (find/rule)
by Anonymous Monk on Apr 22, 2014 at 11:22 UTC

    1st recommandation should be File::Find. In your case

    Are you sure about that? Because that doesn't have /opt/tmp or @files

    My first recommendation is always File::Find::Rule

    use File::Find::Rule qw/ rule find /; my @files = find( file => maxdepth => 1, ascii => in => '/opt/tmp' );
      O.K., you'd have to change '.' from my snippet to '/opt' or '/opt/tmp' or $dir to work it on other than current dir. Whether you use File::Find or File::Find::Rule is more a matter of taste. The maintainer of the module describes it as a is a friendlier interface to File::Find :-).

      Greetings,
      Janek Schleicher

        O.K., you'd have to change ...

        But where is the @files? :)

        Whether you use File::Find or File::Find::Rule is more a matter of taste.

        :) Hmm, @files? There is nothing like @files in File::Find docs ... OTOH File::Find::Rules shows a @files

        Its more than a matter of taste, File::Find::Rules is much much more memorable ... and friendly to noobs

        File::Find::Rules is like cake (sweet), where File::Find is like raw flour

Re^2: Perl List files in directory
by Anonymous Monk on Apr 22, 2014 at 11:40 UTC
    ... already does what you intend...

    All three of the OP's examples don't descend into subdirs, this one does. Also, this code doesn't show which subdir the file was found in, so you'd probably want something like this instead:

    find({wanted => sub {-f and print "file = $File::Find::name\n"},follow => 1},"/opt/tmp");

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found