[Pdx-pm] perlfind.pl

Kris Bosland krisb at ring.org
Mon Aug 8 18:36:37 PDT 2005

Thanks for the help, here is the revised script.  Comments?

use strict;
use warnings;
use File::Find;

#Collect arguments
die "usage: $0 <pattern or perl code> [directories]"
    if not @ARGV or $ARGV[0] =~ /^-[h?]/i;
my $rawmatch = shift;
my @dirs = @ARGV || ('.');

#Create match sub, or use users match sub
my $out = '$File::Find::name\n';
my $debug = 0;
my $match;
	#Need to disable safeguards,
	#and give bare words some extra help,
	#or we loose some bits
	#e.g. perlfind.pl x.z -> xz
	no strict qw{subs};
	no warnings qw{reserved};
	$match = eval $rawmatch;
	$match = eval "qq{$rawmatch}" if ref $match eq '';
	use strict qw{subs};
	use warnings qw{reserved};

#Turn strings into regexp
$match = qr/$match/ if ref $match eq '';
$match = eval "sub { print qq{$out} if /$match/; }" if ref $match eq
die qq{Cannot handle type } . ref $match .
    qq{:"$rawmatch", only types ""(scalar), "Regexp", and "CODE"}
    if ref $match ne 'CODE';

use B::Deparse;
my $deparse = B::Deparse->new("-p", "-sC");
$debug and print $deparse->coderef2text($match), qq{\n};

#Execute find
find($match, @dirs);

>perlfind.pl xyz

>perlfind.pl "x.z"

>perlfind.pl "qr/x.z/"

>perlfind.pl "$out = q{ gotcha: $File::Find::name\n }; qr/x.z/"
 gotcha: ./dir/abc.xyz

>perlfind.pl "$debug = 1; qq{xyz}"
    use warnings;
    use strict 'refs';
    (/(?-xism:xyz)/ and print("$File::Find::name\n"));


More information about the Pdx-pm-list mailing list