Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change


by jrsmith (Pilgrim)
on May 01, 2000 at 17:19 UTC ( #9799=perlquestion: print w/replies, xml ) Need Help??
jrsmith has asked for the wisdom of the Perl Monks concerning the following question:

i need a little help understanding how to use this... i tried looking in the library but i'm still pretty new to perl so alot of it went right over my head, so if anybody could give me a basic rundown on how to use this i'd be eternally grateful :)

Replies are listed 'Best First'.
Re: file::find
by ZZamboni (Curate) on May 01, 2000 at 18:41 UTC
    The first argument to find() is a code reference (like a pointer to a subroutine) and the rest of the arguments are the directories where to search. For each file in those directories, the subroutine is called with the file name as its argument. The subroutine is responsible for checking whatever you want to check, and for doing whatever you want to do. So for example, if you want to print all files ending in .tex under your 'Docs' directory, you could do something like this:
    use File::Find; find( sub { /\.tex$/ && print "$File::Find::name\n" }, 'Docs');
    Note that I used an anonymous subroutine here. The above could have been written as follows:
    find(\&wanted, 'Docs'); sub wanted { /\.tex$/ && print "$File::Find::name\n"; }
    A good way to learn is to use the find2perl program, which should be included in your Perl distribution. It takes the same arguments as the find command, and spits out a Perl program that uses the File::Find module to do the same thing. By looking at the generated programs you can learn a lot about how to use the module.

      Actually, no--the filename isn't supplied as the argument to your subroutine. There aren't any arguments, in fact. (Actually, if you peek at @_ in your wanted function, you *will* see arguments. But they're not actually supplied as arguments to your subroutine: they're the arguments to File::Find's finddir subroutine, which calls your wanted sub as &$wanted, which makes its @_ visible to your subroutine.) More in perlsub.

      But that's getting off track. Anyway, what File::Find does is sets some package variables and $_ to the file name, the directory, and a combination of those two. From the docs:

      $File::Find::dir contains the current directory name, and $_ the current filename within that directory. $File::Find::name contains "$File::Find::dir/$_". You are chdir()'d to $File::Find::dir when the function is called. The function may set $File::Find::prune to prune the tree.
RE: file::find
by mikfire (Deacon) on May 01, 2000 at 21:19 UTC
    Because this one has bitten me so many times, I will warn you in big red letters ( oh the temptation for a little creative html :) that Find::File, in direct contradiction of the Principle Of Least Astonishment (POLA) and in an otherwise stupid fashion, does a chdir on you. You will not end in the same directory you started.

    Either store your starting directory and go back after the File::Find is done, or write your own as I have done.

    Mik Firestone ( perlus bigotus maximus )

      Alternately, use a hashref as the first argument to find() and set the no_chdir option. find {wanted => \&wanted, no_chdir => 1}, '/dir';
      Then, no chdir is done; and $_ will be the same as $File::Find::name.
        I know this is an old post, but the only documentation I found on this was to go into the module and read. There are a few flags in there that aren't mentioned in the documentation

        I don't know if your code works, b/c of the missing parens, but I use:

        find ({wanted => \&wanted, no_chdir=>1}, $dir1);


        Where is that documented? I have looked for that flag for ages. What version? A quick grep of my perl5.005_03 sources do not turn up this flag. Ah. I see. That would be new to perl 5.6.0. Cool! I can stop implementing my own Find. Thanks!

        Mik Firestone ( perlus bigotus maximus )

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://9799]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-02-19 20:18 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (266 votes). Check out past polls.