Skip to content

Commit d78f32f

Browse files
author
Father Chrysostomos
committed
Update docs to concur with $`,$&,$' changes
plus a couple of other pod tweaks.
1 parent 20961b6 commit d78f32f

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

pod/perlre.pod

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ X</p> X<regex, preserve> X<regexp, preserve>
9191
Preserve the string matched such that ${^PREMATCH}, ${^MATCH}, and
9292
${^POSTMATCH} are available for use after matching.
9393

94+
In Perl 5.18 and higher this is ignored. ${^PREMATCH}, ${^MATCH}, and
95+
${^POSTMATCH} will be available after the match regardless of the modifier.
96+
9497
=item g and c
9598
X</g> X</c>
9699

@@ -871,9 +874,11 @@ B<NOTE>: Failed matches in Perl do not reset the match variables,
871874
which makes it easier to write code that tests for a series of more
872875
specific cases and remembers the best match.
873876

874-
B<WARNING>: Once Perl sees that you need one of C<$&>, C<$`>, or
877+
B<WARNING>: If your code is to run on Perl 5.16 or earlier,
878+
beware that once Perl sees that you need one of C<$&>, C<$`>, or
875879
C<$'> anywhere in the program, it has to provide them for every
876-
pattern match. This may substantially slow your program. Perl
880+
pattern match. This may substantially slow your program. (In Perl 5.18 a
881+
more efficient mechanism is used, eliminating any slowdown.) Perl
877882
uses the same mechanism to produce C<$1>, C<$2>, etc, so you also pay a
878883
price for each pattern that contains capturing parentheses. (To
879884
avoid this cost while retaining the grouping behaviour, use the
@@ -882,19 +887,17 @@ use C<$&>, C<$`> or C<$'>, then patterns I<without> capturing
882887
parentheses will not be penalized. So avoid C<$&>, C<$'>, and C<$`>
883888
if you can, but if you can't (and some algorithms really appreciate
884889
them), once you've used them once, use them at will, because you've
885-
already paid the price. As of 5.17.4, the presence of each of the three
886-
variables in a program is recorded separately, and depending on
887-
circumstances, perl may be able be more efficient knowing that only C<$&>
888-
rather than all three have been seen, for example.
890+
already paid the price.
889891
X<$&> X<$`> X<$'>
890892

891-
As a workaround for this problem, Perl 5.10.0 introduces C<${^PREMATCH}>,
893+
As a workaround for this problem, Perl 5.10.0 introduced C<${^PREMATCH}>,
892894
C<${^MATCH}> and C<${^POSTMATCH}>, which are equivalent to C<$`>, C<$&>
893895
and C<$'>, B<except> that they are only guaranteed to be defined after a
894896
successful match that was executed with the C</p> (preserve) modifier.
895897
The use of these variables incurs no global performance penalty, unlike
896898
their punctuation char equivalents, however at the trade-off that you
897-
have to tell perl when you want to use them.
899+
have to tell perl when you want to use them. As of Perl 5.18, these three
900+
variables are equivalent to C<$`>, C<$&> and C<$'>, and C</p> is ignored.
898901
X</p> X<p modifier>
899902

900903
=head2 Quoting metacharacters

pod/perlreref.pod

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,15 @@ There is no quantifier C<{,n}>. That's interpreted as a literal string.
275275
${^MATCH} Entire matched string
276276
${^POSTMATCH} Everything after to matched string
277277

278+
Note to those still using Perl 5.16 or earlier:
278279
The use of C<$`>, C<$&> or C<$'> will slow down B<all> regex use
279-
within your program. Consult L<perlvar> for C<@->
280-
to see equivalent expressions that won't cause slow down.
281-
See also L<Devel::SawAmpersand>. Starting with Perl 5.10, you
280+
within your program. Consult L<perlvar> for C<@->
281+
to see equivalent expressions that won't cause slowdown.
282+
See also L<Devel::SawAmpersand>. Starting with Perl 5.10, you
282283
can also use the equivalent variables C<${^PREMATCH}>, C<${^MATCH}>
283284
and C<${^POSTMATCH}>, but for them to be defined, you have to
284285
specify the C</p> (preserve) modifier on your regular expression.
286+
In Perl 5.18, the use of C<$`>, C<$&> and C<$'> makes no speed difference.
285287

286288
$1, $2 ... hold the Xth captured expr
287289
$+ Last parenthesized pattern match

pod/perlretut.pod

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,10 @@ of the string after the match. An example:
900900

901901
In the second match, C<$`> equals C<''> because the regexp matched at the
902902
first character position in the string and stopped; it never saw the
903-
second 'the'. It is important to note that using C<$`> and C<$'>
903+
second 'the'.
904+
905+
If your code is to run on Perl versions earlier than
906+
5.18, it is worthwhile to note that using C<$`> and C<$'>
904907
slows down regexp matching quite a bit, while C<$&> slows it down to a
905908
lesser extent, because if they are used in one regexp in a program,
906909
they are generated for I<all> regexps in the program. So if raw
@@ -913,8 +916,11 @@ C<@+> instead:
913916
$' is the same as substr( $x, $+[0] )
914917

915918
As of Perl 5.10, the C<${^PREMATCH}>, C<${^MATCH}> and C<${^POSTMATCH}>
916-
variables may be used. These are only set if the C</p> modifier is present.
917-
Consequently they do not penalize the rest of the program.
919+
variables may be used. These are only set if the C</p> modifier is
920+
present. Consequently they do not penalize the rest of the program. In
921+
Perl 5.18, C<${^PREMATCH}>, C<${^MATCH}> and C<${^POSTMATCH}> are available
922+
whether the C</p> has been used or not (the modifier is ignored), and
923+
C<$`>, C<$'> and C<$&> do not cause any speed difference.
918924

919925
=head2 Non-capturing groupings
920926

pod/perlvar.pod

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ we have not made another match:
780780
$1 is Mutt; $2 is Jeff
781781
$1 is Wallace; $2 is Grommit
782782

783-
Due to an unfortunate accident of Perl's implementation, C<use
783+
If you are using Perl v5.16 or earlier, note that C<use
784784
English> imposes a considerable performance penalty on all regular
785785
expression matches in a program because it uses the C<$`>, C<$&>, and
786786
C<$'>, regardless of whether they occur in the scope of C<use
@@ -797,6 +797,9 @@ Since Perl v5.10.0, you can use the C</p> match operator flag and the
797797
C<${^PREMATCH}>, C<${^MATCH}>, and C<${^POSTMATCH}> variables instead
798798
so you only suffer the performance penalties.
799799

800+
If you are using Perl v5.18.0 or higher, you do not need to worry about
801+
this, as the three naughty variables are no longer naughty.
802+
800803
=over 8
801804

802805
=item $<I<digits>> ($1, $2, ...)
@@ -819,7 +822,8 @@ The string matched by the last successful pattern match (not counting
819822
any matches hidden within a BLOCK or C<eval()> enclosed by the current
820823
BLOCK).
821824

822-
The use of this variable anywhere in a program imposes a considerable
825+
In Perl v5.16 and earlier, the use of this variable
826+
anywhere in a program imposes a considerable
823827
performance penalty on all regular expression matches. To avoid this
824828
penalty, you can extract the same substring by using L</@->. Starting
825829
with Perl v5.10.0, you can use the C</p> match flag and the C<${^MATCH}>
@@ -833,9 +837,11 @@ Mnemonic: like C<&> in some editors.
833837
X<${^MATCH}>
834838

835839
This is similar to C<$&> (C<$MATCH>) except that it does not incur the
836-
performance penalty associated with that variable, and is only guaranteed
840+
performance penalty associated with that variable.
841+
In Perl v5.16 and earlier, it is only guaranteed
837842
to return a defined value when the pattern was compiled or executed with
838-
the C</p> modifier.
843+
the C</p> modifier. In Perl v5.18, the C</p> modifier does nothing, so
844+
C<${^MATCH}> does the same thing as C<$MATCH>.
839845

840846
This variable was added in Perl v5.10.0.
841847

@@ -850,7 +856,8 @@ The string preceding whatever was matched by the last successful
850856
pattern match, not counting any matches hidden within a BLOCK or C<eval>
851857
enclosed by the current BLOCK.
852858

853-
The use of this variable anywhere in a program imposes a considerable
859+
In Perl v5.16 and earlier, the use of this variable
860+
anywhere in a program imposes a considerable
854861
performance penalty on all regular expression matches. To avoid this
855862
penalty, you can extract the same substring by using L</@->. Starting
856863
with Perl v5.10.0, you can use the C</p> match flag and the
@@ -865,9 +872,11 @@ Mnemonic: C<`> often precedes a quoted string.
865872
X<$`> X<${^PREMATCH}>
866873

867874
This is similar to C<$`> ($PREMATCH) except that it does not incur the
868-
performance penalty associated with that variable, and is only guaranteed
875+
performance penalty associated with that variable.
876+
In Perl v5.16 and earlier, it is only guaranteed
869877
to return a defined value when the pattern was compiled or executed with
870-
the C</p> modifier.
878+
the C</p> modifier. In Perl v5.18, the C</p> modifier does nothing, so
879+
C<${^PREMATCH}> does the same thing as C<$PREMATCH>.
871880

872881
This variable was added in Perl v5.10.0
873882

@@ -886,7 +895,8 @@ enclosed by the current BLOCK). Example:
886895
/def/;
887896
print "$`:$&:$'\n"; # prints abc:def:ghi
888897

889-
The use of this variable anywhere in a program imposes a considerable
898+
In Perl v5.16 and earlier, the use of this variable
899+
anywhere in a program imposes a considerable
890900
performance penalty on all regular expression matches.
891901
To avoid this penalty, you can extract the same substring by
892902
using L</@->. Starting with Perl v5.10.0, you can use the C</p> match flag
@@ -901,9 +911,11 @@ Mnemonic: C<'> often follows a quoted string.
901911
X<${^POSTMATCH}> X<$'> X<$POSTMATCH>
902912

903913
This is similar to C<$'> (C<$POSTMATCH>) except that it does not incur the
904-
performance penalty associated with that variable, and is only guaranteed
914+
performance penalty associated with that variable.
915+
In Perl v5.16 and earlier, it is only guaranteed
905916
to return a defined value when the pattern was compiled or executed with
906-
the C</p> modifier.
917+
the C</p> modifier. In Perl v5.18, the C</p> modifier does nothing, so
918+
C<${^POSTMATCH}> does the same thing as C<$POSTMATCH>.
907919

908920
This variable was added in Perl v5.10.0.
909921

0 commit comments

Comments
 (0)