The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

File::Find::Closures - functions you can use with File::Find

SYNOPSIS

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->();

DESCRIPTION

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.

The closure factories

Each factory returns two closures. The first one is for find(), and the second one is the reporter.

find_by_created_after( EPOCH_TIME );

Find files created after EPOCH_TIME, which is in seconds since the local epoch (I may need to adjust this for some operating systems).

find_by_created_before( EPOCH_TIME );

Find files created before EPOCH_TIME, which is in seconds since the local epoch (I may need to adjust this for some operating systems).

find_by_directory_contains( @names );

Find directories which contain files with the same name as any of the values in @names.

find_by_executable();

Find files that are executable. This may not work on some operating systems (like Windows) unless someone can provide me with an alternate version.

find_by_extension( EXTENSIONS )

This function removes any leading . from each value in EXTENSIONS, so these are the same:

my( $finder, $reporter ) = find_by_extension( 't' );
my( $finder, $reporter ) = find_by_extension( '.t' );

Internal dots are left alone:

my( $finder, $reporter ) = find_by_extension( 'tar.gz' );
find_by_group( GROUP_NAME | GROUP_GID );

Find files that are owned by the owner with the name GROUP_NAME. You can also use the group's GID.

find_by_max_size( SIZE );

Find files whose size is equal to or less than SIZE bytes.

find_by_min_size( SIZE );

Find files whose size is equal to or greater than SIZE bytes.

find_by_modified_after( EPOCH_TIME );

Find files modified after EPOCH_TIME, which is in seconds since the local epoch (I may need to adjust this for some operating systems).

find_by_modified_before( EPOCH_TIME );

Find files modified before EPOCH_TIME, which is in seconds since the local epoch (I may need to adjust this for some operating systems).

find_by_name( @names );

Find files with the names in @names. The result is the name returned by $File::Find::name normalized by File::Spec::canonfile().

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.

find_by_owner( OWNER_NAME | OWNER_UID );

Find files that are owned by the owner with the name OWNER_NAME. You can also use the owner's UID.

find_by_regex( REGEX );

Find files whose name match REGEX.

This function does not use no_updirs, so if you ask for "." or "..", that's what you get.

find_regular_files();

Find all regular files.

find_by_umask( UMASK );

Find files that fit the umask UMASK. The files will not have those permissions.

find_by_zero_size();

Find files whose size is equal to 0 bytes.

find_by_writeable();

Find files that are writable. This may not work on some operating systems (like Windows) unless someone can provide me with an alternate version.

ADD A CLOSURE

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.

TO DO

* more functions!

* need input on how things like mod times work on other operating systems

SEE ALSO

File::Find

Randal Schwartz's File::Finder, which does the same task but differently.

SOURCE AVAILABILITY

This module is in Github:

https://github.com/briandfoy/file-find-closures.git

AUTHOR

brian d foy, <bdfoy@cpan.org>

Some functions implemented by Nathan Wagner, <nw@hydaspes.if.org>

COPYRIGHT AND LICENSE

Copyright © 2004-2024, brian d foy <bdfoy@cpan.org>. All rights reserved.

You may redistribute this under the same terms as the Artistic License 2.0.