diff options
author | Karl Williamson <khw@khw-desktop.(none)> | 2010-06-24 08:06:27 -0600 |
---|---|---|
committer | David Golden <dagolden@cpan.org> | 2010-07-17 21:50:48 -0400 |
commit | 9d8606788fa6ee1bda9ff32c9ae6693c93631733 (patch) | |
tree | a39cab2457361234e2f63b80f7fbf4faf391828e /pod/perlre.pod | |
parent | 0b70ce6f951c268ebdde6747ec43ca06875ec918 (diff) | |
download | perl-9d8606788fa6ee1bda9ff32c9ae6693c93631733.tar.gz |
Add examples to perlre on perils of not using \g{}
These come from Abigail.
Signed-off-by: David Golden <dagolden@cpan.org>
Diffstat (limited to 'pod/perlre.pod')
-rw-r--r-- | pod/perlre.pod | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/pod/perlre.pod b/pod/perlre.pod index 704878780b..5af167b820 100644 --- a/pod/perlre.pod +++ b/pod/perlre.pod @@ -454,7 +454,8 @@ a backreference only if at least 11 left parentheses have opened before it. And so on. C<\1> through C<\9> are always interpreted as backreferences. You can minimize the ambiguity by always using C<\g> if you mean capturing groups; and always using 3 digits for octal constants, with the first always "0" (which -works if there are 63 (= \077) or fewer capture groups). +works if there are 63 (= \077) or fewer capture groups). There are several +examples below that illustrate these perils. The C<\I<digit>> notation also works in certain circumstances outside the pattern. See L</Warning on \1 Instead of $1> below for details.) @@ -478,6 +479,20 @@ Examples: $seconds = $3; } + /(.)(.)(.)(.)(.)(.)(.)(.)(.)\g10/ # \g10 is a backreference + /(.)(.)(.)(.)(.)(.)(.)(.)(.)\10/ # \10 is octal + /((.)(.)(.)(.)(.)(.)(.)(.)(.))\10/ # \10 is a backreference + /((.)(.)(.)(.)(.)(.)(.)(.)(.))\010/ # \010 is octal + + $a = '(.)\1'; # Creates problems when concatenated. + $b = '(.)\g{1}'; # Avoids the problems. + "aa" =~ /${a}/; # True + "aa" =~ /${b}/; # True + "aa0" =~ /${a}0/; # False! + "aa0" =~ /${b}0/; # True + "aa\x8" =~ /${a}0/; # True! + "aa\x8" =~ /${b}0/; # False + Several special variables also refer back to portions of the previous match. C<$+> returns whatever the last bracket match matched. C<$&> returns the entire matched string. (At one point C<$0> did |