raygun has asked for the wisdom of the Perl Monks concerning the following question:
Hello wise monks,
I'm unsure why these two lines produce different output. In particular, I don't know why the ${^MATCH} in the first line fails to be set to the matched part of the regular expression.Perl v5.12.4 produces:perl -e '$x = "abcde"; $x =~ s/b../==${^MATCH}==/p; print + "$x\n"' perl -e '$x = "abcde"; $sub="b.."; $x =~ s/$sub/==${^MATCH}==/p; print + "$x\n"'
The perlre documentation gives no restrictions on contexts in which {^MATCH} can be used, saying only that it's an optimized synonym for $&. And in fact, if I change the first line to use $& instead of {^MATCH}, it behaves as expected. Thank you for any hints!a====e a==bcd==e
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: ${^MATCH} sometimes fails in target of substitution?
by dave_the_m (Monsignor) on Jun 16, 2013 at 18:34 UTC | |
|
Re: ${^MATCH} sometimes fails in target of substitution?
by space_monk (Chaplain) on Jun 16, 2013 at 17:38 UTC | |
by ikegami (Patriarch) on Jun 17, 2013 at 08:16 UTC | |
by space_monk (Chaplain) on Jun 17, 2013 at 10:11 UTC | |
|
Re: ${^MATCH} sometimes fails in target of substitution?
by farang (Chaplain) on Jun 16, 2013 at 18:10 UTC |