|
Packit |
26bf30 |
#!perl -w
|
|
Packit |
26bf30 |
use strict;
|
|
Packit |
26bf30 |
use File::Find::Object::Rule;
|
|
Packit |
26bf30 |
use File::Spec::Functions qw(catdir);
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
# bootstrap extensions
|
|
Packit |
26bf30 |
for (@INC) {
|
|
Packit |
26bf30 |
my $dir = catdir($_, qw( File Find Rule ) );
|
|
Packit |
26bf30 |
next unless -d $dir;
|
|
Packit |
26bf30 |
my @pm = find( name => '*.pm', maxdepth => 1,
|
|
Packit |
26bf30 |
exec => sub { my $name = $_[0]; $name =~ s/\.pm$//;
|
|
Packit |
26bf30 |
eval "require File::Find::Object::Rule::$name"; },
|
|
Packit |
26bf30 |
in => $dir );
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
# what directories are we searching in?
|
|
Packit |
26bf30 |
my @where;
|
|
Packit |
26bf30 |
while (@ARGV) {
|
|
Packit |
26bf30 |
local $_ = shift @ARGV;
|
|
Packit |
26bf30 |
if (/^-/) {
|
|
Packit |
26bf30 |
unshift @ARGV, $_;
|
|
Packit |
26bf30 |
last;
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
push @where, $_;
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
# parse arguments, build a rule object
|
|
Packit |
26bf30 |
my $rule = new File::Find::Object::Rule;
|
|
Packit |
26bf30 |
while (@ARGV) {
|
|
Packit |
26bf30 |
my $clause = shift @ARGV;
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
unless ( $clause =~ s/^-// && $rule->can( $clause ) ) {
|
|
Packit |
26bf30 |
# not a known rule - complain about this
|
|
Packit |
26bf30 |
die "unknown option '$clause'\n"
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
# it was the last switch
|
|
Packit |
26bf30 |
unless (@ARGV) {
|
|
Packit |
26bf30 |
$rule->$clause();
|
|
Packit |
26bf30 |
next;
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
# consume the parameters
|
|
Packit |
26bf30 |
my $param = shift @ARGV;
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
if ($param =~ /^-/) {
|
|
Packit |
26bf30 |
# it's the next switch - put it back, and add one with no params
|
|
Packit |
26bf30 |
unshift @ARGV, $param;
|
|
Packit |
26bf30 |
$rule->$clause();
|
|
Packit |
26bf30 |
next;
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
if ($param eq '(') {
|
|
Packit |
26bf30 |
# multiple values - just look for the closing parenthesis
|
|
Packit |
26bf30 |
my @p;
|
|
Packit |
26bf30 |
while (@ARGV) {
|
|
Packit |
26bf30 |
my $val = shift @ARGV;
|
|
Packit |
26bf30 |
last if $val eq ')';
|
|
Packit |
26bf30 |
push @p, $val;
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
$rule->$clause( @p );
|
|
Packit |
26bf30 |
next;
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
# a single argument
|
|
Packit |
26bf30 |
$rule->$clause( $param );
|
|
Packit |
26bf30 |
}
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
# add a print rule so things happen faster
|
|
Packit |
26bf30 |
$rule->exec( sub { print "$_[2]\n"; return; } );
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
# profit
|
|
Packit |
26bf30 |
$rule->in( @where ? @where : '.' );
|
|
Packit |
26bf30 |
exit 0;
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
__END__
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=head1 NAME
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
findorule - command line wrapper to File::Find::Object::Rule
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=head1 USAGE
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
findorule [path...] [expression]
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=head1 DESCRIPTION
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
C<findorule> mostly borrows the interface from GNU find(1) to provide a
|
|
Packit |
26bf30 |
command-line interface onto the File::Find::Object::Rule heirarchy of modules.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
The syntax for expressions is the rule name, preceded by a dash,
|
|
Packit |
26bf30 |
followed by an optional argument. If the argument is an opening
|
|
Packit |
26bf30 |
parenthesis it is taken as a list of arguments, terminated by a
|
|
Packit |
26bf30 |
closing parenthesis.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
Some examples:
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
find -file -name ( foo bar )
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
files named C<foo> or C<bar>, below the current directory.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
find -file -name foo -bar
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
files named C<foo>, that have pubs (for this is what our ficticious
|
|
Packit |
26bf30 |
C<bar> clause specifies), below the current directory.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
find -file -name ( -bar )
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
files named C<-bar>, below the current directory. In this case if
|
|
Packit |
26bf30 |
we'd have omitted the parenthesis it would have parsed as a call to
|
|
Packit |
26bf30 |
name with no arguments, followed by a call to -bar.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=head2 Supported switches
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
I'm very slack. Please consult the File::Find::Object::Rule manpage for now,
|
|
Packit |
26bf30 |
and prepend - to the commands that you want.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=head2 Extra bonus switches
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
findorule automatically loads all of your installed File::Find::Object::Rule::*
|
|
Packit |
26bf30 |
extension modules, so check the documentation to see what those would be.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=head1 AUTHOR
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
Richard Clamp <richardc@unixbeard.net> from a suggestion by Tatsuhiko Miyagawa
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
Adapted to L<File::Find::Object::Rule> by Shlomi Fish (all copyrights
|
|
Packit |
26bf30 |
disclaimed).
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=head1 COPYRIGHT
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
Copyright (C) 2002 Richard Clamp. All Rights Reserved.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
This program is free software; you can redistribute it and/or modify it
|
|
Packit |
26bf30 |
under the same terms as Perl itself.
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=head1 SEE ALSO
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
L<File::Find::Object::Rule>
|
|
Packit |
26bf30 |
|
|
Packit |
26bf30 |
=cut
|