[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?

perlfind.pl
======================================================================
#!/usr/intel/bin/perl
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
'Regexp';
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);
======================================================================
examples:

>perlfind.pl xyz
./dir/abc.xyz

>perlfind.pl "x.z"
./dir/abc.xyz

>perlfind.pl "qr/x.z/"
./dir/abc.xyz

>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"));
}
./dir/abc.xyz


-Kris



More information about the Pdx-pm-list mailing list