While practicing my newly acquired wisdom, I tried this
#! perl -w use strict; $|++; my $source = 'blahperlblahjavablah'; my ($f1, $f2) = $source =~ /blah(\w+)blah(\w+)blah/; # first attempt print "#1==$1==$2==\n"; print "#2==$f1==$f2==\n"; $source = 'blahblahblah'; ($f1, $f2) = $source =~ /blah(\w+)blah(\w+)blah/; # second attempt print "#3==$1==$2==\n"; print "#4==$f1==$f2==\n"; printf "#5==%s\n", unpack "H*", "$2";
which produced the following output
H:\devperl\perlmonks>scoping-assign.pl #1==perl==java== #2==perl==java== #3==blah== ava== #4====== #5==00617661
OK, prints #1 and #2 confirm the obvious: the first attempt produces 2 successful matches, reflected both in variables $1, $2 and in my variables $f1, $f2.
The second attempt produces no matches, which is is reflected correctly in my variables $f1, $f2 (shown in #4), while the variables $1, $2 would be unchanged - or so I expected.

But, look, $2 was changed, as shown in #3, and confirmed in #5: the first character was replaced by ascii 0!

Can anyone confirm and explain this? I believed that $1, etc, variables stay unchanged when the second match attempt is not successful. But it seems that they can contain garbage.

Obtained on Win2k, AS perl 5.6.0 build 623.

Rudif

In reply to 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.