From 9d8606788fa6ee1bda9ff32c9ae6693c93631733 Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Thu, 24 Jun 2010 08:06:27 -0600 Subject: [PATCH] Add examples to perlre on perils of not using \g{} These come from Abigail. Signed-off-by: David Golden --- pod/perlre.pod | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pod/perlre.pod b/pod/perlre.pod index 7048787..5af167b 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> notation also works in certain circumstances outside the pattern. See L 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 -- 1.8.3.1