find2perl and File::Find examples

S

Sunil

All,
Where can I find good ( & simple) examples of find2perl and File::Find.
The perldoc documentation is not very helpful (for me).


Thanks,
Sunil.
 
R

Randal L. Schwartz

Sunil> Where can I find good ( & simple) examples of find2perl and
Sunil> File::Find. The perldoc documentation is not very helpful
Sunil> (for me).

It might help us if you elaborate the dividing line between the parts
you understand and the parts you don't. In other words, what's your
question to us?

Do you understand find(1) already?
Do you know what a coderef is?
Do you know what a hashref is?
Do you understand what a recursive filetree walk does?

What task were you trying to accomplish that you failed at?

See, when someone says "This isn't enough doc", I've got to wonder
what they would want.
 
M

Michele Dondi

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top