Here is my find example. I use this when I need to remember how to use find. Course I didn't know about File::Find::Rule! Good to know.
use strict;
use File::Find;
print "starting a find for all files:\n";
my $files = myfind('.');
printf "Found %d files.\n", scalar(@{$files});
print "starting a find for all files matching critera:\n";
$files = myfind('.', \&criteria);
printf "Found %d files.\n", scalar(@{$files});
# critera function is called by find for each file.
# passed the filename found, and must return boolean
# indicating whether to include the file in the result
# or not.
sub criteria
{
my $filename = shift;
return $filename =~ /example/i;
}
# find function that will return an array or array reference
# containing the files that match the criteria.
# accepts two parameters:
# directory to search
# optional criteria function.
#
sub myfind
{
my $dir = shift;
my $criteriaFunc = shift || sub{1;};
my $list = [];
find({wanted=>sub{findcb($list, $criteriaFunc)}}, $dir);
return((wantarray)?(@$list):($list));
}
# callback function called by find each time a file is found.
# this callback will add the file to the listref if it matches
# the critera.
sub findcb
{
my $listref = shift;
my $criteriaFunc = shift;
if($criteriaFunc->($File::Find::name))
{
push @$listref, $File::Find::name;
}
}
"Look, Shiny Things!" is not a better business strategy than compatibility and reuse.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|