Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: File::Find: Return array of "wanted" files

by keszler (Priest)
on Oct 04, 2013 at 07:44 UTC ( #1056848=note: print w/ replies, xml ) Need Help??


in reply to File::Find: Return array of "wanted" files

Because File::Find's "wanted function" does not return the file names, the array that will hold the matching files needs to be in scope, e.g. a package or global variable.

Note: File::Find's find function takes an array as its 2nd parameter, so foreach (@dirs) isn't needed.

use strict; use warnings; use File::Find; my @files; my @dirs = qw(/some /list /of /dirs); sub findstuff { my $file = $File::Find::name; return unless -f "$file"; if (grep { /regex/ } "$file") { print "Found regex in $file\n"; push @files, $file; } } find(\&findstuff, @dirs); print "Results:\n@files\n";
Update: Interesting: I interpreted the OP's if (fgrep { /regex/ } "$file") as a typo (fgrep vs grep) for a check for a regex in the filename. choroba's response below makes better sense of that.


Comment on Re: File::Find: Return array of "wanted" files
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2015-07-06 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (73 votes), past polls