Skip to content

Commit

Permalink
Mention that the regex used by the empty m// is scoped
Browse files Browse the repository at this point in the history
  • Loading branch information
E. Choroba committed Nov 1, 2023
1 parent 8a16188 commit 050872c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pod/perlop.pod
Original file line number Diff line number Diff line change
Expand Up @@ -2055,15 +2055,16 @@ The bottom line is that using C</o> is almost never a good idea.
=item The empty pattern C<//>

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

The last successful pattern may be accessed as a variable via
C<${^LAST_SUCCESSFUL_PATTERN}>. Matching against it, or the empty
Expand Down
1 change: 1 addition & 0 deletions pod/perlvar.pod
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ documented as behaving otherwise. See the following section for more
details.

=head3 Scoping Rules of Regex Variables
X<Scoping Rules of Regex Variables>

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

0 comments on commit 050872c

Please sign in to comment.