J
John Doe
I'm stumped! Debugger indicates that the value of $timestamp changes
from something to '' on the 2nd "if" line below. If the "my
$timestamp" declaration is moved up out of the while loop so that
$timestamp is global, it works. Why?
Re-read that section of the best book on Perl [1] and read perlsub on
"my."
####################################################################
# For each line in the error log:
#
while (<ERRORLOG>) {
chomp;
my $timestamp; # IS THIS SCOPE NOT FOR THE ENTIRE WHILE LOOP?
#
# Trap the last timestamp line before the error:
#
if (/Mon|Tue|Wed|Thu|Fri|Sat|Sun/) {
$timestamp = "$`$&$'"; # IT GETS SET HERE OK.
}
if (/fail|bad|oops/) { # ...BUT HERE IT BECOMES '' (null)!!
print "$timestamp: "; # THIS PRINTS NOTHING!
print "$`$&$'";
}
} # end of while loop
####################################################################
FYI, my version of Perl is:
$ perl -version
This is perl, v5.8.1 built for i386-linux-thread-multi
from something to '' on the 2nd "if" line below. If the "my
$timestamp" declaration is moved up out of the while loop so that
$timestamp is global, it works. Why?
Re-read that section of the best book on Perl [1] and read perlsub on
"my."
####################################################################
# For each line in the error log:
#
while (<ERRORLOG>) {
chomp;
my $timestamp; # IS THIS SCOPE NOT FOR THE ENTIRE WHILE LOOP?
#
# Trap the last timestamp line before the error:
#
if (/Mon|Tue|Wed|Thu|Fri|Sat|Sun/) {
$timestamp = "$`$&$'"; # IT GETS SET HERE OK.
}
if (/fail|bad|oops/) { # ...BUT HERE IT BECOMES '' (null)!!
print "$timestamp: "; # THIS PRINTS NOTHING!
print "$`$&$'";
}
} # end of while loop
####################################################################
FYI, my version of Perl is:
$ perl -version
This is perl, v5.8.1 built for i386-linux-thread-multi