develooper Front page | perl.perl5.porters | Postings from July 2014

[perl #122380] k regex modifier is not documented in `perlre` or `perlreref`

Thread Next
From:
Mark Stosberg
Date:
July 24, 2014 06:41
Subject:
[perl #122380] k regex modifier is not documented in `perlre` or `perlreref`
Message ID:
rt-4.0.18-11152-1406167225-1098.122380-75-0@perl.org
# New Ticket Created by  Mark Stosberg 
# Please include the string:  [perl #122380]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=122380 >


I saw this code in Regexp::Common, and didn't recognize the "k" modifier, so I looked it up:

    (?k:...)

/k is not found in perlre:
http://perldoc.perl.org/perlre.html

In `perlreref`, the complete list of flags is given as:

    m/pattern/msixpogcdualse

(No "k")

I eventually found that the /k flag appears to have been added in 2007, and the behavior is described in the patch that submitted it:

http://www.nntp.perl.org/group/perl.perl5.porters/2007/01/msg119878.html

Here's the full description from that patch, pasted in:

###
Historically the utility of $`, $&, and $' has been restricted by the
performance penalty that their usage imposes on every match that will
be executed in the program. The main reason for this is that they have
to be available any time, and given the dynamic nature of perl there
is no way to tell when and therefore we play it safe. (COW semantics
might improve things if somebody is interested.)

So this patch implements what I hope is a reasonable workaround. It
provides aliases for the vars, ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH},
which dont impose the global performance penalty, and it provides a
new regex modifier, /k, which ensures that the vars are defined when
needed. Thus the variables are only guaranteed to be defined after a
succesfull match that has been compiled/executed with the /k modifier.

(Currently they are also defined after a successful match that
contains capture buffers, but I didnt think it was smart to promise
this behaviour in the light that how capture buffers are handled could
be changed to be more efficient in the future, and guaranteeing that
they would allow $^MATCH and friedns could impede such optimisation.)

This incidentally means that the /k modifier behaves a little
different from the /msix modifers, in that there is no "negative" or
"disable" form. A /k modifier turns on the "keep-a-copy on successful
match behaviour", something which can not be turned off. Thus

  print qr/foo/k;

outputs (?k-msix:foo) but

  print qr/foo/;

outputs (?-msix:foo)

Using a negative -k mdofier in a pattern is ignored, but will produce
warnings if they are enabled. If you think about it a while this makes
sense as with something like:

  my $qr=qr/(?k)(?{print ${^MATCH})/;

the effect of the /k has to apply to any pattern that the $qr gets embedded in.
###


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About