Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Marshall's scratchpad

by Marshall (Abbot)
on Dec 26, 2008 at 13:00 UTC ( #732670=scratchpad: print w/replies, xml ) Need Help??

Using File::Find

This tutorial shows how to use the File::Find module.

File::Find is a core module, meaning that it is already installed on all Perl systems - no installation is required! If the system has Perl, File::Find is already there!

The find() function is included within that File::Find module. The first step is to tell Perl that you want to use this pre-installed module - that will make the subroutine "find()" known to the rest of the code.

Getting Started:

You need to include a use File::Find; statement at the top of the code, this "imports" the "find" fuction and makes it known to the rest of the code.

What the find() routine does...

The man (manual) page - shown by (perldoc file::find) or via Google search says:

use File::Find;
find(\&wanted, @directories_to_search);
sub wanted { ... }

I really don't why this "wanted" name got used, but that is a reference to a subroutine name, followed by a list of directory names.

The find() function will cwd (change working directory) into the the first directory in that list and do a readdir(). It will then call the "wanted()" function for every single name from that readdir() list. It might be a directory, it might be a simple file, it might be some kind of linked file/directory (not ususal, but possible).

After find() does that, it will follow the directory names, meaning that it will cwd (change working directory) into the next directory name and continue following it downward calling the "wanted()" function on every "name" whether it be a directory or a simple file or some kind of link.

If we have:

use File::Find; find (\&for_every_name, (C:/temp, C:/Windows); my @allFiles; sub for_every_name { print "$File::Find::name\n"; # note there is no "return value" # it is not possible to return anything from this # subroutine # If you want that: push @allFiles, $File::Find::name; # you must save information in a varialble that has higher # scope than this subroutine # There is NO return value from this subroutine! }
The above will print the names of all files (and directories and symbolic links) underneath C:/temp and C:/WINDOWS (NOTE: Windows file systems are case insensitive, but Unix file systems are case sensitive - so be aware of that!).

Special Variables

Within the subroutine that the find() routine calls, a couple of special variables to tell you where it is "at":

-$File::Find::name - the name of the file that the "wanted()" function is currently looking at.

-$File::Find:dir - the name of the current directoryt that the wanted() function is currently within.

Log In?

What's my password?
Create A New User
[erix]: Alvin Lee (Ten Years After) played a jazzguitar ("Big Red"), a good warm sound
[stonecolddevin]: I think Stevie Ray is one of maybe 2 or 3 people that could take a Hendrix song and make you go "...huh. this is close." https://open. 0UVM9trirBK5s1xBwL oVhZ
[stonecolddevin]: https://open. 0UVM9trirBK5s1xBwL oVhZ rather
[Corion]: Sadly I must go to bed now :-| See y'all!
[erix]: I really loved/love this "Recorded Live" record, 1973 (yes, I'm that old)
[stonecolddevin]: goodnight Corion
[erix]: cya Corion :)
[stevieb]: Joan Jett, Hendrix, Neil Young, Dave Keuning (The Killers)... I have my mind elsewhere so I'm limited in my capacity to think about this righ tnow ;)
[erix]: another blues with Big Red :)
[stonecolddevin]: I will say John Mayer is an excellent guitarist, I don't like his music but he's pretty damn good

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2017-06-22 21:37 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.