As I prefer functional style programming over procedural, I never really liked File::Find's "global vars interface".
So being faced with the problem of looking through some directory tree i will use File::Find, but when it's necessary to tweak the process of its search, I prefer using the preprocess CODEref.
As I don't see that covered a lot in the resources given by PodMaster, I'd like to give an (overly explicit) example:
use strict;
use warnings;
use File::Find;
sub wanted {
my $file = $File::Find::name;
print "$File::Find::name is a ", -d $file ? 'directory' : 'file', $/
+;
}
sub filter {
grep {
my $file = "$File::Find::dir/$_";
if (-d $file) {
my $depth = $file =~ s#/#/#g;
if ($depth > 3) {
print "Depth > 3 in $file ... skipping\n";
0; # return as grep result for this $file
} else {
1; # concider this to recurse into
}
} else {
1; # include non-dirs
}
} grep !/^\.\.?$/, @_;
}
find(
{
no_chdir => 1,
wanted => \&wanted,
preprocess => \&filter,
},
shift
);
An additional advantage of this approach over using/setting
File::Find::prune, is that it also works with
bydepth => 1.
Also note, that I generally refuse to let find do the chdir thing, because that tends to confuse me. ;-)
Just my two eurocent
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.