On Aug 15, Keith Olmstead said:
>foreach $log (@logfile) {
> find (\&eachFile, "$startdir");
>} # End for loop
>sub eachFile {
> if (-e $_ && $_ =~ /$log$/) { push @log, $File::Find::name;}
>} # End eachFile
Everyone has been telling you to use strict, and scope the $log variable,
but no one has run the code they've corrected, or they'd spot that scoping
$log in the for loop makes in NOT visible in the eachFile() function.
Here's a simple test:
for my $x (1, 2, 3) {
print_x();
}
sub print_x {
print "x = $x\n";
}
The output is "x = " three times. If warnings were on, you'd be told that
an uninitialized value was being used. If strict was on, you'd be told
that $x requires a package name on the line where it's being printed.
And you can't get around this just by doing
my $x;
for $x (1, 2, 3) {
print_x();
}
either, because the $x in the for loop is STILL lexically scoped to the
INSIDE of the loop!
One way to get around it would be to do:
my $x;
for (1, 2, 3) {
$x = $_;
print_x();
}
And another way would be to inline the print_x() function. In your case,
these two approaches would look like this:
my ($find_log_dir, @valid);
for (@logfiles) {
$find_log_dir = $_;
find(\&each_file, $startdir);
}
sub each_file {
push @valid, $File::Find::name if /$find_log_dir$/;
}
and
my @valid;
for my $dir (@logfiles) {
find(sub { push @valid, $File::Find::name if /$dir$/ }, $startdir);
}
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]