The first argument of find() is a reference to a subroutine. This is where you tell find what you want done with the filenames it finds. Find will pass the names it finds into $_ . Say you wanted to print a list of all the files below your current directory:

find sub{print $_,"\n"}, "./"

The second parameter to find() takes an array of directories. If you only have one directory, as above, that is fine. Remember that perl passes parameters in a flat list so a single scalar will pass the same as an array of one element.

You can try this from the command line to get familiar with the concept:

%perl -MFile::Find -e 'find sub{print $_,"\n"}, "./"'

Of course find() really shines when iterating over a list of directories: find sub ( do-something }, @directories Note: This can return a lot of files as find() works recursively down through all the sub directories.

Find does not return the found files so this:  @array = find(); Doesn't work. You have to store the files passed to you yourself if you want to keep them. Like this: find sub { push @filelist, $_ }, @directories;

Of course if you subroutine is more complicated then it can be written separately and a reference to it passed to find().

sub my_Sub{ do-something-with-$_ . . } find \&my_Sub, @directories;


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

In reply to Re: how to use file::Find ? by starbolin
in thread how to use file::Find ? by steph_bow

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.