This has got to be a bug. You're not going crazy. The $DIGIT variables should NOT get altered if there is no match. This is documented:
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:
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 =
But this program doesn't:
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 =
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).

HOLY (expletive)! I just uncovered something very bad about Perl. Please watch:

($_ = "ABCD") =~ /(..)(..)/; print "$1, $2\n"; $_ = "WXYZ"; print "$1, $2\n"; __END__ AB, CD AB, CD
That looks fine, right? Now watch THIS:
() = ($_ = "ABCD") =~ /(..)(..)/; print "$1, $2\n"; $_ = "WXYZ"; print "$1, $2\n"; __END__ AB, CD WX, YZ
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.