Skip to content

Commit 050872c

Browse files
author
E. Choroba
committed
Mention that the regex used by the empty m// is scoped
1 parent 8a16188 commit 050872c

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

pod/perlop.pod

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,15 +2055,16 @@ The bottom line is that using C</o> is almost never a good idea.
20552055
=item The empty pattern C<//>
20562056

20572057
If the I<PATTERN> evaluates to the empty string, the last
2058-
I<successfully> matched regular expression is used instead. In this
2059-
case, only the C<g> and C<c> flags on the empty pattern are honored; the
2060-
other flags are taken from the original pattern. If no match has
2061-
previously succeeded, this will (silently) act instead as a genuine
2062-
empty pattern (which will always match). Using a user supplied string as
2063-
a pattern has the risk that if the string is empty that it triggers the
2064-
"last successful match" behavior, which can be very confusing. In such
2065-
cases you are recommended to replace C<m/$pattern/> with
2066-
C<m/(?:$pattern)/> to avoid this behavior.
2058+
I<successfully> matched regular expression in the current dynamic
2059+
scope is used instead (see also L<perlvar/Scoping Rules of Regex Variables>).
2060+
In this case, only the C<g> and C<c> flags on the empty pattern are
2061+
honored; the other flags are taken from the original pattern. If no
2062+
match has previously succeeded, this will (silently) act instead as a
2063+
genuine empty pattern (which will always match). Using a user supplied
2064+
string as a pattern has the risk that if the string is empty that it
2065+
triggers the "last successful match" behavior, which can be very
2066+
confusing. In such cases you are recommended to replace C<m/$pattern/>
2067+
with C<m/(?:$pattern)/> to avoid this behavior.
20672068

20682069
The last successful pattern may be accessed as a variable via
20692070
C<${^LAST_SUCCESSFUL_PATTERN}>. Matching against it, or the empty

pod/perlvar.pod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ documented as behaving otherwise. See the following section for more
967967
details.
968968

969969
=head3 Scoping Rules of Regex Variables
970+
X<Scoping Rules of Regex Variables>
970971

971972
Regular expression variables allow the programmer to access the state of
972973
the most recent I<successful> regex match in the current dynamic scope.

0 commit comments

Comments
 (0)