I think this has to do with optimization of the regexp. I may not be using terminology quite right, but Perl sees that the $sub variable can be interpolated, so the regexp is eval'd at compile time, thereby populating ${^MATCH}. That is, it is only after being eval'd once that ${^MATCH} gets the value from the search.
The following code also forces a similar 'second eval'.
my $x = "abcde"; $x =~ s/b../sprintf "==${^MATCH}=="/pe; print "$x\n";
In reply to Re: ${^MATCH} sometimes fails in target of substitution?
by farang
in thread ${^MATCH} sometimes fails in target of substitution?
by raygun
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |