$ {^ MATCH} and the / p modifier in perl v24

perlre says:

R

Save the line so that $ {^ PREMATCH}, $ {^ MATCH} and $ {^ POSTMATCH} are available for use after matching.

In Perl 5.20 and later, this is ignored. Due to the new copy to the recording mechanism, $ {^ PREMATCH}, $ {^ MATCH} and $ {^ POSTMATCH} will be available after the match regardless of the modifier.

also perlvar

$ {^ MATCH} This is similar to $ & ($ MATCH), except that it is not associated with this variable.

In Perl v5.18 and earlier, only a certain value is returned when the template was compiled or executed using the / p modifier. In Perl v5.20, the / p modifier does nothing, so $ {^ MATCH} does the same as $ MATCH.

Test:

echo 'Lorem ipsum dolor sit ut dicta qui dolores.' |\
 perl -nE 'say ${^MATCH} while m/dolor/g'

: (2x \n)

:

echo 'Lorem ipsum dolor sit ut dicta qui dolores.' |\
 perl -nE 'say ${^MATCH} while m/dolor/gp'

:

dolor
dolor

perl:

$ perl -v

This is perl 5, version 24, subversion 0 (v5.24.0) built for darwin-2level
(with 1 registered patch, see perl -V for more detail)

, 5.24.0 /p? ?

+6
1

. RT # 131087.

$& ($MATCH).

+6

Source: https://habr.com/ru/post/1016239/


All Articles