Paid for writing

In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks

Thursday, April 2, 2009

Using File::Find in Perl

This tutorial will explain the usage of File::Find Perl Module

The source code is as follows :

#!c:\perl\bin\

use File::Find;

@directories_to_search=("D:/");
find(\&wanted,@directories_to_search);

sub wanted
{

#print "Looking in $File::Find::dir.....\n";
if($_ =~ /\.pl$/)
#if($_ =~ /^find.*/)
{
print "Found : $_ in $File::Find::dir\n";
}
}



Explanation

The File::Find can be used in your code by writing the 'use' statement.
The use statement will ensure that you can use the functions defined under File::Find.
Now, the find() function requires the list of directories where you want it to search.
I have entered D:\ as the only directory (drive in this case) as the location where I want to search. Multiple names can be separated with a comma.

The find() function calls the wanted() during each of its traversal. What find() basically does is a Depth First Search over the list of directories which have been mentioned earlier.
The wanted() is called for every file found within that directory.

Now, wanted() function is where you can filter out the type of file you want to Find.
What I am looking for over here is all perl files. So what I have done is written a regular expression of this form -- /\.pl$/
This regular expression searches if the file which is being traversed ends with .pl
If it ends with .pl then it prints the file name and the directory name where it is located.
This is where you come across three inbuilt variables
1. $File::Find::dir -- gives the name of the current directory which is being traversed.
2. $_ -- gives the file name only which is currently being traversed.
4. $File::Find::name -- this is combination of $File::Find::dir and $_ thereby giving the full path name for the file.

So there you go inside the if() I am checking whether $_ matches the regular expression for .pl and if it matches then I am printing it.
This regular expression can vary, like for instance the regular expression which is in comment. /^find.*/
This regular expression will search for all files beginning with the name 'find'. This is like doing 'dir find*' on windows command line or 'ls find*' on Unix.

1 comments:

Wolf said...

Perl is the best scripting language for Text processing and handle regex. I have posted few articles related to those at my blog

http://icfun.blogspot.com/search/label/perl

Also Perl's Cpan has lots of support that I don't even need to think extra while developing project. I didn't find such help on other programming language except Java and .NET