in reply to Re^3: A bug in Perl regex(?)
in thread A bug in Perl regex(?)

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