Bewildered: "my" variable drops out of scope

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
 
P

Paul Lalli

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
####################################################################


$timestamp's scope is not "for the entire while loop" as you said. It is
actually for one iteration of the loop. At the end of each iteration,
$timestamp goes out of scope. When the next iteration begins, a new
$timestamp is redeclared, having the undefined value. That's why you're
seeing different values for $timestamp within the two if statements -
you're looking at different iterations.

On a side note, why are you using the performance-hitting $`, $&, and $'
variables? Especially when you're just squishing them back together to
reform whatever was in $_ to begin with...

Paul Lalli
 
U

Uri Guttman

JD> while (<ERRORLOG>) {
JD> chomp;
JD> my $timestamp; # IS THIS SCOPE NOT FOR THE ENTIRE WHILE LOOP?
JD> #
JD> # Trap the last timestamp line before the error:
JD> #
JD> if (/Mon|Tue|Wed|Thu|Fri|Sat|Sun/) {
JD> $timestamp = "$`$&$'"; # IT GETS SET HERE OK.

don't use $& as it will slow down other regexes in your code. this has
been fixed i think for very recent perls but it is still not good style
IMO.


JD> }
JD> if (/fail|bad|oops/) { # ...BUT HERE IT BECOMES '' (null)!!

are you sure it is passing that regex AND the previous one for each line
you process?

JD> print "$timestamp: "; # THIS PRINTS NOTHING!

it isn't out of scope for if that were true you would get a compile time
error (i assume strict is enabled since you are using my).

JD> print "$`$&$'";

no need for 2 prints there, one longer string is fine.

uri
 
T

Tassilo v. Parseval

Also sprach Uri Guttman:
don't use $& as it will slow down other regexes in your code. this has
been fixed i think for very recent perls but it is still not good style
IMO.

As far as I know its bad effects have just been softened a little (but
I am not even sure that this has happened). bleadperl's perlvar.pod
still gives the old suggestion not to use it.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top