| 1 |
#!perl -w |
| 2 |
use strict; |
| 3 |
use File::Find::Rule; |
| 4 |
use File::Spec::Functions qw(catdir); |
| 5 |
|
| 6 |
# bootstrap extensions |
| 7 |
for (@INC) { |
| 8 |
my $dir = catdir($_, qw( File Find Rule ) ); |
| 9 |
next unless -d $dir; |
| 10 |
my @pm = find( name => '*.pm', maxdepth => 1, |
| 11 |
exec => sub { (my $name = $_) =~ s/\.pm$//; |
| 12 |
eval "require File::Find::Rule::$name"; }, |
| 13 |
in => $dir ); |
| 14 |
} |
| 15 |
|
| 16 |
# what directories are we searching in? |
| 17 |
my @where; |
| 18 |
while (@ARGV) { |
| 19 |
local $_ = shift @ARGV; |
| 20 |
if (/^-/) { |
| 21 |
unshift @ARGV, $_; |
| 22 |
last; |
| 23 |
} |
| 24 |
push @where, $_; |
| 25 |
} |
| 26 |
|
| 27 |
# parse arguments, build a rule object |
| 28 |
my $rule = new File::Find::Rule; |
| 29 |
while (@ARGV) { |
| 30 |
my $clause = shift @ARGV; |
| 31 |
|
| 32 |
unless ( $clause =~ s/^-// && $rule->can( $clause ) ) { |
| 33 |
# not a known rule - complain about this |
| 34 |
die "unknown option '$clause'\n" |
| 35 |
} |
| 36 |
|
| 37 |
# it was the last switch |
| 38 |
unless (@ARGV) { |
| 39 |
$rule->$clause(); |
| 40 |
next; |
| 41 |
} |
| 42 |
|
| 43 |
# consume the parameters |
| 44 |
my $param = shift @ARGV; |
| 45 |
|
| 46 |
if ($param =~ /^-/) { |
| 47 |
# it's the next switch - put it back, and add one with no params |
| 48 |
unshift @ARGV, $param; |
| 49 |
$rule->$clause(); |
| 50 |
next; |
| 51 |
} |
| 52 |
|
| 53 |
if ($param eq '(') { |
| 54 |
# multiple values - just look for the closing parenthesis |
| 55 |
my @p; |
| 56 |
while (@ARGV) { |
| 57 |
my $val = shift @ARGV; |
| 58 |
last if $val eq ')'; |
| 59 |
push @p, $val; |
| 60 |
} |
| 61 |
$rule->$clause( @p ); |
| 62 |
next; |
| 63 |
} |
| 64 |
|
| 65 |
# a single argument |
| 66 |
$rule->$clause( $param ); |
| 67 |
} |
| 68 |
|
| 69 |
# add a print rule so things happen faster |
| 70 |
$rule->exec( sub { print "$_[2]\n"; return; } ); |
| 71 |
|
| 72 |
# profit |
| 73 |
$rule->in( @where ? @where : '.' ); |
| 74 |
exit 0; |
| 75 |
|
| 76 |
__END__ |
| 77 |
|
| 78 |
=head1 NAME |
| 79 |
|
| 80 |
findrule - command line wrapper to File::Find::Rule |
| 81 |
|
| 82 |
=head1 USAGE |
| 83 |
|
| 84 |
findrule [path...] [expression] |
| 85 |
|
| 86 |
=head1 DESCRIPTION |
| 87 |
|
| 88 |
C<findrule> mostly borrows the interface from GNU find(1) to provide a |
| 89 |
command-line interface onto the File::Find::Rule heirarchy of modules. |
| 90 |
|
| 91 |
The syntax for expressions is the rule name, preceded by a dash, |
| 92 |
followed by an optional argument. If the argument is an opening |
| 93 |
parenthesis it is taken as a list of arguments, terminated by a |
| 94 |
closing parenthesis. |
| 95 |
|
| 96 |
Some examples: |
| 97 |
|
| 98 |
find -file -name ( foo bar ) |
| 99 |
|
| 100 |
files named C<foo> or C<bar>, below the current directory. |
| 101 |
|
| 102 |
find -file -name foo -bar |
| 103 |
|
| 104 |
files named C<foo>, that have pubs (for this is what our ficticious |
| 105 |
C<bar> clause specifies), below the current directory. |
| 106 |
|
| 107 |
find -file -name ( -bar ) |
| 108 |
|
| 109 |
files named C<-bar>, below the current directory. In this case if |
| 110 |
we'd have omitted the parenthesis it would have parsed as a call to |
| 111 |
name with no arguments, followed by a call to -bar. |
| 112 |
|
| 113 |
=head2 Supported switches |
| 114 |
|
| 115 |
I'm very slack. Please consult the File::Find::Rule manpage for now, |
| 116 |
and prepend - to the commands that you want. |
| 117 |
|
| 118 |
=head2 Extra bonus switches |
| 119 |
|
| 120 |
findrule automatically loads all of your installed File::Find::Rule::* |
| 121 |
extension modules, so check the documentation to see what those would be. |
| 122 |
|
| 123 |
=head1 AUTHOR |
| 124 |
|
| 125 |
Richard Clamp <[email protected]> from a suggestion by Tatsuhiko Miyagawa |
| 126 |
|
| 127 |
=head1 COPYRIGHT |
| 128 |
|
| 129 |
Copyright (C) 2002 Richard Clamp. All Rights Reserved. |
| 130 |
|
| 131 |
This program is free software; you can redistribute it and/or modify it |
| 132 |
under the same terms as Perl itself. |
| 133 |
|
| 134 |
=head1 SEE ALSO |
| 135 |
|
| 136 |
L<File::Find::Rule> |
| 137 |
|
| 138 |
=cut |