The scope of $<digit> (and $`, $&, and $') extends to the end of the enclosing BLOCK or eval string, or to the next successful pattern match, whichever comes first.Here's the odd thing. This program works as expected:
But this program doesn't:use strict; my ($f1,$f2); ($f1, $f2) = 'XaaXbbX' =~ /X(\w+)X(\w+)X/; print "\$1 = $1; \$2 = $2\n"; print "\$f1 = $f1; \$f2 = $f2\n"; ($f1, $f2) = 'XXX' =~ /X(\w+)X(\w+)X/; print "\$1 = $1; \$2 = $2\n"; print "\$f1 = $f1; \$f2 = $f2\n"; __END__ $1 = aa; $2 = bb $f1 = aa; $f2 = bb $1 = aa; $2 = bb $f1 = ; $f2 =
Hmm, it seems to have something to do with the variable. Oh, and running use re 'debug' on this code shows that the second regex NEVER GETS DONE (this is a good thing, too, since that second regex demands 5 characters at least, and there are only 3, so Perl knows not to do it).use strict; my ($f1,$f2); $_ = 'XaaXbbX'; ($f1, $f2) = /X(\w+)X(\w+)X/; # first attempt print "\$1 = $1; \$2 = $2\n"; print "\$f1 = $f1; \$f2 = $f2\n"; $_ = 'XXX'; ($f1, $f2) = /X(\w+)X(\w+)X/; # first attempt print "\$1 = $1; \$2 = $2\n"; print "\$f1 = $f1; \$f2 = $f2\n"; __END__ $1 = aa; $2 = bb $f1 = aa; $f2 = bb $1 = XX; $2 = bb $f1 = ; $f2 =
HOLY (expletive)! I just uncovered something very bad about Perl. Please watch:
That looks fine, right? Now watch THIS:($_ = "ABCD") =~ /(..)(..)/; print "$1, $2\n"; $_ = "WXYZ"; print "$1, $2\n"; __END__ AB, CD AB, CD
This shows that when you (supposedly) store the returned parenthetical matches from a pattern match, Perl LINKS the digit variables to SECTIONS of the string! This is probably less than good.() = ($_ = "ABCD") =~ /(..)(..)/; print "$1, $2\n"; $_ = "WXYZ"; print "$1, $2\n"; __END__ AB, CD WX, YZ
This happens in 5.005_02, as well as 5.6.0. I'll submit a bug report.
japhy --
Perl and Regex Hacker
In reply to Re: Another regex variable puzzle
by japhy
in thread Another regex variable puzzle
by Rudif
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |