|
File::Find::Closures - functions you can use with File::Find |
File::Find::Closures - functions you can use with File::Find
use File::Find;
use File::Find::Closures qw(:all);
my( $wanted, $list_reporter ) = find_by_name( qw(README) );
File::Find::find( $wanted, @directories );
File::Find::find( { wanted => $wanted, ... }, @directories );
my @readmes = $list_reporter->();
I wrote this module as an example of both using closures and using File::Find. Students are always asking me what closures are good for, and here's some examples. The functions mostly stand alone (i.e. they don't need the rest of the module), so rather than creating a dependency in your code, just lift the parts you want).
When I use File::Find, I have two headaches---coming up with the \&wanted function to pass to find(), and acculumating the files.
This module provides the \&wanted functions as a closures that I can pass directly to find(). Actually, for each pre-made closure, I provide a closure to access the list of files too, so I don't have to create a new array to hold the results.
The filenames are the full path to the file as reported by File::Find.
Unless otherwise noted, the reporter closure returns a list of the filenames in list context and an anonymous array that is a copy (not a reference) of the original list. The filenames have been normalized by File::Spec::canonfile unless otherwise noted. The list of files has been processed by File::Spec::no_upwards so that ``.'' and ``..'' (or their equivalents) do not show up in the list.
Each factory returns two closures. The first one is for find(), and the second one is the reporter.
In list context, it returns the list of files. In scalar context,, it returns an anonymous array.
This function does not use no_updirs, so if you ask for ``.'' or ``..'', that's what you get.
This function does not use no_updirs, so if you ask for ``.'' or ``..'', that's what you get.
I want to add as many of these little functions as I can, so please send me ones that you create!
You can follow the examples in the source code, but here is how you should write your closures.
You need to provide both closures. Start of with the basic subroutine
stub to do this. Create a lexical array in the scope of the subroutine.
The two closures will share this variable. Create two closures: one
of give to find() and one to access the lexical array.
sub find_by_foo
{
my @args = @_;
my @found = ();
my $finder = sub { push @found, $File::Find::name if ... };
my $reporter = sub { @found };
return( $finder, $reporter );
}
The filename should be the full path to the file that you get
from $File::Find::name, unless you are doing something wierd,
like find_by_directory_contains().
Once you have something, send it to me at <bdfoy@cpan.org>. You
must release your code under the Perl Artistic License.
* more functions!
* need input on how things like mod times work on other operating systems
Randal Schwartz's File::Finder, which does the same task but
differently.
This source is part of a SourceForge project which always has the latest sources in CVS, as well as all of the previous releases.
http://sourceforge.net/projects/brian-d-foy/
If, for some reason, I disappear from the world, one of the other members of the project can shepherd this module appropriately.
brian d foy, <bdfoy@cpan.org>
Some functions implemented by Nathan Wagner, <nw@hydaspes.if.org>
Copyright (c) 2004-2007, brian d foy, All Rights Reserved.
You may redistribute this under the same terms as Perl itself.
|
File::Find::Closures - functions you can use with File::Find |