NAME
File::Find::Match - Perform different actions on files based on file name.
SYNOPSIS
#!/usr/bin/perl
use strict;
use warnings;
use File::Find::Match qw( :constants );
use File::Find::Match::Sugar qw( dir default );
my $finder = new File::Find::Match;
$finder->rules(
".svn" => sub { IGNORE },
qr/\.pm$/ => sub {
print "Perl module: $_\n";
MATCH;
},
qr/\.pl$/ => sub {
print "This is a perl script: $_\n";
# let the following rules have a crack at it.
},
qr/filer\.pl$/ => sub {
print "myself!!! $_\n";
MATCH;
},
# "dir {" is the same as "sub { -d $_ } => sub {"
dir {
print "Directory: $_\n";
MATCH;
},
# default is like an else clause for an if statement.
# It is run if none of the other rules return MATCH or IGNORE.
default {
print "Default handler.\n";
MATCH;
},
);
$finder->find('.');
DESCRIPTION
This module is allows one to recursively process files and directories based on the filename. It is meant to be more flexible than File::Find.
METHODS
new(%opts)
Creates a new File::Find::Match
object. Currently %opts is ignored.
If you are going to sublcass File::Find::Match
, you may override initialize() which new() calls right after object creation.
rules($predicate => $action, ...)
rules() accpets a list of $predicate => $action pairs.
See "RULES" for a detailed description.
rule($predicate => $action)
This is just an alias to rules().
find(@dirs)
Start the breadth-first search of @dirs (defaults to '.' if empty) using the specified rules.
The return value of this function is unimportant.
EXPORTS
Two constants are exported: IGNORE
and MATCH
.
See "Actions" for usage.
RULES
A rule is a predicate => action pair.
A predicate is the code (or regexp, see below) used to determine if we want to process a file.
An action is the code we use to process the file. By process, I mean anything from sending it through a templating engine to printing its name to STDOUT
.
Predicates
A predicate is one of: a Regexp reference from qr//
, a subroutine reference, or a string. An action is a subroutine reference that is called on a filename when a predicate matches it.
Naturally for regexp predicates, matching occures when the pattern matches the filename.
For coderef predicates, $_ is set to the filename and the subroutine is called. If it returns a true value, the predicate is true. Else the predicate is false.
When the predicate is a string, it must match the basename of $_ (e.g. filename sans path) exactly. For example, "foo" will match "bar/foo", "bar/baz/foo", and "bar/baz/quux/foo".
Actions
An action is just a subroutine reference that is called when its associated predicate matches a file. When an action is called, $_ will be set to the filename.
If an action returns IGNORE
or MATCH
, all following rules will not be tried. You should return IGNORE
when you do not want to recurse into a directory, and MATCH
otherwise. On non-directories, currently there is no difference between the two.
If an action returns niether IGNORE
nor MATCH
, the next rule will be tried.
AUTHOR
Dylan William Hardison <dhardison@cpan.org>
SEE ALSO
File::Find::Match::Sugar
, File::Find, perl(1).
COPYRIGHT and LICENSE
Copyright (C) 2004 Dylan William Hardison. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.