L
LBJ
Can someone please help me understand what I'm seeing as a
contradiction between a statement in the perlre man page and a bug in
a script that just bit me in the a**?
From perlre:
"The numbered variables ($1, $2, $3, etc.) and the related
punctuation set ($+, $&, $`, and $') are all dynamically
scoped until the end of the enclosing block or until the
next successful match, whichever comes first."
And here's the code in question:
# looping through a fairly standard apache log
while (<LOGFILE>)
{
# grab the datestamp and querystring
m/\s\[([^\s]+) \-\d+\] "[A-Z]+ [^\?\s]+\?([^\s]+)/;
my ($date, $querystring) = ($1, $2);
if ($querystring)
{
....
}
}
The "bug" here is that $2 seems to not get unset for each iteration of
the while loop. When the loop gets to a line that doesn't match a
querystring value, $querystring is getting assigned the value of the
previous line's $2 capture buffer.
I've always been under the impression that the capture buffers got
unset with each subsequent regular expression, regardless of success
or failure. If that's not true, I still don't underdstand how this
gels with the statement in the perlre man page about the special
variables being dynamically scoped until the end of the block.
Thanks,
Jay L.
contradiction between a statement in the perlre man page and a bug in
a script that just bit me in the a**?
From perlre:
"The numbered variables ($1, $2, $3, etc.) and the related
punctuation set ($+, $&, $`, and $') are all dynamically
scoped until the end of the enclosing block or until the
next successful match, whichever comes first."
And here's the code in question:
# looping through a fairly standard apache log
while (<LOGFILE>)
{
# grab the datestamp and querystring
m/\s\[([^\s]+) \-\d+\] "[A-Z]+ [^\?\s]+\?([^\s]+)/;
my ($date, $querystring) = ($1, $2);
if ($querystring)
{
....
}
}
The "bug" here is that $2 seems to not get unset for each iteration of
the while loop. When the loop gets to a line that doesn't match a
querystring value, $querystring is getting assigned the value of the
previous line's $2 capture buffer.
I've always been under the impression that the capture buffers got
unset with each subsequent regular expression, regardless of success
or failure. If that's not true, I still don't underdstand how this
gels with the statement in the perlre man page about the special
variables being dynamically scoped until the end of the block.
Thanks,
Jay L.