|
NAMEFile::Find::Closures - functions you can use with File::FindSYNOPSISuse 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->(); DESCRIPTIONI 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. The closure factoriesEach factory returns two closures. The first one is for find(), and the second one is the reporter.
ADD A CLOSUREI 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. TO DO* more functions!* need input on how things like mod times work on other operating systems SEE ALSOFile::FindRandal Schwartz's "File::Finder", which does the same task but differently. SOURCE AVAILABILITYThis module is in Github:https://github.com/briandfoy/file-find-closures.git AUTHORbrian d foy, "<bdfoy@cpan.org>"Some functions implemented by Nathan Wagner, "<nw@hydaspes.if.org>" COPYRIGHT AND LICENSECopyright © 2004-2021, brian d foy <bdfoy@cpan.org>. All rights reserved.You may redistribute this under the same terms as the Artistic License 2.0.
Visit the GSP FreeBSD Man Page Interface. |