General
cancel
Showing results for 
Search instead for 
Did you mean: 

Perl script problem - file::find

SOLVED
Go to solution
Nick Brennan_2
Occasional Advisor

Perl script problem - file::find

Hi,

Was hoping one of you would be able to point me in the right direction, I know this probably isn't the right place to be posting it....

Am writing a perl admin script, which needs to find a bunch of files passed as parameters to the script:



My script basically looks like this:

use strict;
use File::Find;
my $dir = "$ARGV[0]" ;
shift @ARGV;
foreach (@ARGV) {
find(\&wanted, '$dir', '$_');
}
sub wanted { /$ARGV[1]/ && print "$File::Find::name\n;" }

It doesn't work. Have tried various other methods... Can anyone give me a quick pointer how to go about this?

TIA
Nick
Ain't life grand......
4 REPLIES
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl script problem - file::find

--8<---
use strict;
use File::Find;

sub usage
{
print STDERR "usage: $0 dir file ...\n";
exit;
} # usage

my $dir = shift;
-d $dir or usage;
@ARGV or usage;

my $files = join "|" => @ARGV;
$files = qr{$files};

find (sub { m/$files/ and print "$File::Find::name\n" }, $dir);
-->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Nick Brennan_2
Occasional Advisor

Re: Perl script problem - file::find

Thanks a million. Didn't realise you could run tests without parenthesis either.

(Sorry, am a Perl newbie. What's ?).

Thanks again
Nick
Ain't life grand......
H.Merijn Brand (procura
Honored Contributor

Re: Perl script problem - file::find

qr is the quote-regex operator, which precompiles the regular expression so it ain't eveluated time after time

Your major `fault' however was the argument passing in

foreach (@ARGV) {
find (\&wanted, '$dir', '$_');
}

where $dir should NOT be single quoted, because then you are trying to parse the folder that is called $dir literally (and I bet you that that folder/dir does not exist), instead of deparsing the folder that is named by the *content* of the $dir variable

second problem you created is to pass '$_', which again should not have been quoted for now obvious reasons, but find takes the following arguments:

find (sub, dir, ...)

so the first arg is a sub reference, which can be a reference to a named sub, a variable that holds the reference to a named sub or closure, or a closure, which was what I used in my example.
the second and consequetive arguments are dirs or folders, and you tried to pass a filename (tried, because you single quoted it, causing the *literal* $_ to be passed) as third argument, something find cannot descend into

for qr, do

# man perlop

search for

Regexp Quote-Like Operators

to read
--8<---
qr/STRING/imosx
This operator quotes (and possibly compiles) its STRING as a
regular expression. STRING is interpolated the same way as
PATTERN in "m/PATTERN/". If "'" is used as the delimiter, no
interpolation is done. Returns a Perl value which may be used
instead of the corresponding "/STRING/imosx" expression.

For example,

$rex = qr/my.STRING/is;
s/$rex/foo/;

is equivalent to

s/my.STRING/foo/is;
-->8---

Hope this helps.
Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Nick Brennan_2
Occasional Advisor

Re: Perl script problem - file::find

Rather helpful stuff from contributors. Thank God for the HP forums........
Ain't life grand......