Marshall's scratchpadby Marshall (Monsignor)
|on Dec 26, 2008 at 13:00 UTC||Need Help??|
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.
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
use File::Find;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:
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!).
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.