Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > find2perl and File::Find examples

Reply
Thread Tools

find2perl and File::Find examples

 
 
Sunil
Guest
Posts: n/a
 
      11-28-2003
All,
Where can I find good ( & simple) examples of find2perl and File::Find.
The perldoc documentation is not very helpful (for me).


Thanks,
Sunil.



 
Reply With Quote
 
 
 
 
Sunil
Guest
Posts: n/a
 
      11-28-2003
> All,
> Where can I find good ( & simple) examples of find2perl and

File::Find.
> The perldoc documentation is not very helpful (for me).
>


I found
http://www.hk8.org/old_web/perl/sysadmin/ch02_04.htm and
http://perlmonks.thepen.com/217166.html


Addtions are welcome,
Sunil.



 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      11-28-2003
>>>>> "Sunil" == Sunil <(E-Mail Removed)> writes:

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.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      11-28-2003
On Fri, 28 Nov 2003 13:59:02 +0530, "Sunil"
<(E-Mail Removed)> wrote:

> 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
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
differences between find2perl and find GU Perl Misc 4 08-31-2009 08:38 PM
find2perl Ed Doyle Perl 1 12-29-2005 06:31 PM
help with find2perl Sunil Perl Misc 3 11-25-2003 12:31 PM



Advertisments