Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask


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 :)

Comment on file::find
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.
        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 )

        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);


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://9799]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-11-28 01:56 GMT
Find Nodes?
    Voting Booth?

    My preferred Perl binaries come from:

    Results (191 votes), past polls