On Friday 15 August 2003 19:09, Jeff 'japhy' Pinyan wrote:
>
> 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); }
And another way would be:
my $logfile = qr/(?:cron|messages|maillog|ldap)$/;
find( \&eachFile, $startdir );
sub eachFile {
push @log, $File::Find::name if /$logfile/;
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]