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.
- \w+ matches "ab"
- () sets $2
- Print $2
- Second at \w+ fails to match: backtrack
- \w+ matches "a"
- () sets $2 (This isn't happening)
- Print $2
- \w+ matches "b"
- () sets $2
- 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
|