A successful capture should not be undef. Whether it's documented or not, $1, etc are intended to be available to (?{}) and (??{}) blocks, so they must be set ASAP.

  1. \w+ matches "ab"
  2. () sets $2
  3. Print $2
  4. Second at \w+ fails to match: backtrack
  5. \w+ matches "a"
  6. () sets $2 (This isn't happening)
  7. Print $2
  8. \w+ matches "b"
  9. () sets $2
  10. Print $2

I do not see a compelling argument for "it should be the last thing matched by those 'physical' parentheses" over my proposal.

If that's true, then you would expect either of

$2=ab # Only thing matched $2=a # Only thing matched $2=a # First thing matched

and

$2=ab # Only thing matched $2=a # Only thing matched $2=b # Last thing matched

and the code is still buggy as it produces neither.

Clearer bug demonstration:

$ perl -e'"ab" =~ /((\w+)(?{print defined $^N ? "\$^N=$^N\n" : "\$^N n +ot defined\n"})){2}/;' $^N=ab $^N not defined $^N=b

In reply to Re^4: A bug in Perl regex(?) by ikegami
in thread A bug in Perl regex(?) by Serge314

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.