File-Finder
view release on metacpan or search on metacpan
lib/File/Finder/Steps.pm view on Meta::CPAN
package File::Finder::Steps;
our $VERSION = '1.01';
use strict;
use Carp qw(croak);
=head1 NAME
File::Finder::Steps - steps for File::Finder
=head1 SYNOPSIS
## See File::Finder for normal use of steps
## subclassing example:
BEGIN {
package My::File::Finder;
use base File::Finder;
sub _steps_class { "My::File::Finder::Steps" }
}
BEGIN {
package My::File::Finder::Steps;
use base File::Finder::Steps;
sub bigger_than { # true if bigger than N bytes
my $self = shift;
my $bytes = shift;
return sub {
-s > $bytes;
}
}
}
my $over_1k = My::File::Finder->bigger_than(1024);
print "Temp files over 1k:\n";
$over_1k->ls->in("/tmp");
=head1 DESCRIPTION
C<File::Finder::Steps> provide the predicates being tested for
C<File::Finder>.
=head2 STEPS METHODS
These methods are called on a class or instance to add a "step". Each
step adds itself to a list of steps, returning the new object. This
allows you to chain steps together to form a formula.
As in I<find>, the default operator is "and", and short-circuiting is
performed.
Note: the C<user>, C<nouser>, C<group>, C<nogroup>, and C<ls> methods
are not available on Win32 systems.
=over
=item or
Like I<find>'s C<or>.
=cut
sub or { return "or" }
=item left
Like a left parenthesis. Used in nesting pairs with C<right>.
=cut
sub left { return "left" }
BEGIN { *begin = \&left; }
=item right
Like a right parenthesis. Used in nesting pairs with C<left>.
For example:
my $big_or_old = File::Finder
->type('f')
->left
->size("+100")->or->mtime("+90")
->right;
find($big_or_old->ls, "/tmp");
You need parens because the "or" operator is lower precedence than
the implied "and", for the same reason you need them here:
find /tmp -type f '(' -size +100 -o -mtime +90 ')' -print
Without the parens, the -type would bind to -size, and not to the
choice of -size or -mtime.
Mismatched parens will not be found until the formula is used, causing
a fatal error.
=cut
sub right { return "right" }
BEGIN { *end = \&right; }
=item begin
Alias for C<left>.
=item end
Alias for C<right>.
=item not
Like I<find>'s C<!>. Prefix operator, can be placed in front of
individual terms or open parens. Can be nested, but what's the point?
# list all non-files in /tmp
File::Finder->not->type('f')->ls->in("/tmp");
=cut
sub not { return "not" }
=item true
Always returns true. Useful when a subexpression might fail, but
you don't want the overall code to fail:
... ->left-> ...[might return false]... ->or->true->right-> ...
Of course, this is the I<find> command's idiom of:
find .... '(' .... -o -true ')' ...
=cut
sub true { return sub { 1 } }
=item false
Always returns false.
=cut
sub false { return sub { 0 } }
=item comma
Like GNU I<find>'s ",". The result of the expression (or
subexpression if in parens) up to this point is discarded, and
execution continues afresh. Useful when a part of the expression is
needed for its side effects, but shouldn't affect the rest of the
"and"-ed chain.
# list all files and dirs, but don't descend into CVS dir contents:
File::Finder->type('d')->name('CVS')->prune->comma->ls->in('.');
( run in 1.117 second using v1.01-cache-2.11-cpan-437f7b0c052 )