Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: help needed with File::Find and arrays. by osunderdog
in thread help needed with File::Find and arrays. by basarix

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2023-03-31 08:38 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (75 votes). Check out past polls.