Where can I find good ( & simple) examples of find2perl and File::Find.
The perldoc documentation is not very helpful (for me).
I can't understand what the issue could be with the documentation.
IMHO it's very clear. Do you have anything particular in mind?
Also, examples of 'find2perl' are more or less "plain" examples of
'find', with the exception that you can further customize the code
beyond the limits of the 'find' utility.
Personally, by seeing the code posted here every now and again I think
that in many cases it could be worth (but is not done!) to use the
\%options form of the function call and in particular the preprocess
and postprocess "methods".
I don't know it the following examples of mine are good (& simple) -
for sure they are very naive (& old), but in any case here are my
2cents:
#!/usr/bin/perl -i.bak
use strict;
use warnings;
use File::Find;
die "Usage: $0 <dir> [<dir>...]\n" unless @ARGV;
-d or die "`$_' either doesn't exist or is not a directory\n" for
@ARGV;
File::Find::find { preprocess => \&sel,
wanted => \&extr }, @ARGV;
sub sel { grep { /\.(?:asp|html?)/i or -d } @_ }
sub extr {
return unless -f;
print "Processing `$File::Find::name'\n";
local @ARGV=$_;
s/(\.asp)\?/$1_/gi, print while <>;
}
__END__
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Copy;
our ($name, $dir);
*name=*File::Find::name;
*dir=*File::Find::dir;
my %counts;
die "Usage: $0 <dir> [<dir>...]\n" unless @ARGV;
-d or die "`$_' either doesn't exist or is not a directory\n" for
@ARGV;
File::Find::find { preprocess => \&mkbakdir,
wanted => \&bakfile,
postprocess =>\&report }, @ARGV;
sub mkbakdir {
return if $_ eq '.bak';
if (-e '.bak') {
die "`.bak' exist in $dir but is not a directory\n"
unless -d '.bak';
} else {
mkdir '.bak'
}
@_;
}
sub bakfile {
return unless -f;
my $dest='.bak/' . $_;
unlink $dest or
die "Can't remove previous backup copy of `$name': $!\n"
if -e $dest;
copy $_, $dest or
die "Can't copy `$name' to .bak dir: $!\n";
print "`$name' copied to .bak dir\n";
$counts{$dir}++;
}
sub report {
$counts{$dir}=0 unless defined $counts{$dir};
print 'made backup copies for ', $counts{$dir},
' files in ', $dir, "\n";
}
__END__
Michele