![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
use File::Recurse; use File::Copy;
recurse { print } "/tmp"; recurse {copy($_,"elsewhere") if -f $_} "dir"; recurse(\&func, "/");
File::Recurse
module is designed for performing an operation on a tree of directories and
files. The basic usage is simmilar to the find.pl library. Once one uses the File::Recurse module, you need only call the recurse
function in order to perform recursive directory operations.
The function takes two parameters a function reference and a directory. The function referenced by the first parameter should expect to take one parameter: the full path to the file currently being operated on. This function is called once for every file and directory under the directory named by the second parameter.
For example:
recurse(\&func, "/");
would start at the top level of the filesystem and call ``func'' for every file and directory found (not including ``/'').
Perl allows a second form of calling this function which can be useful for situations where you want to do something simple in the function. In these cases, you can define an anonymous function by using braces like so:
recurse {print $_[0]} "/";
This would print every file and directory in the filesystem. However, as an
added convenience you can access the pathname in the variable
$_
. So the above could be rewritten as:
recurse { print } "/";
If the function returns -1 and the current filename refers to a directory, recurse will not descend into that directory. This can be used to prune searches and focus only on those directories which should be followed.
If the function returns -2, the search is terminated, and recurse will return. This can be used to bail when a problem occurs, and you don't want to exit the program, or to end the search for some file once it is found.
$CommentsMailTo = "perl5@dcs.ed.ac.uk"; include("../syssies_footer.inc");?>