in reply to find function
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 );
Also note, that I generally refuse to let find do the chdir thing, because that tends to confuse me. ;-)
Just my two eurocent
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: find function
by Anonymous Monk on Dec 06, 2005 at 16:09 UTC | |
by pKai (Priest) on Dec 06, 2005 at 16:40 UTC |