Change 20997 by [EMAIL PROTECTED] on 2003/09/02 14:58:21

        Subject: [PATCH perlfaq6.pod] Explain \Q better
        From: Mark Jason Dominus <[EMAIL PROTECTED]>
        Date: Mon, 01 Sep 2003 16:19:20 -0400
        Message-ID: <[EMAIL PROTECTED]>

Affected files ...

... //depot/perl/pod/perlfaq6.pod#42 edit

Differences ...

==== //depot/perl/pod/perlfaq6.pod#42 (text) ====
Index: perl/pod/perlfaq6.pod
--- perl/pod/perlfaq6.pod#41~18459~     Wed Jan  8 12:48:19 2003
+++ perl/pod/perlfaq6.pod       Tue Sep  2 07:58:21 2003
@@ -292,14 +292,26 @@
 also that any regex special characters will be acted on unless you
 precede the substitution with \Q.  Here's an example:
 
-    $string = "to die?";
-    $lhs = "die?";
-    $rhs = "sleep, no more";
+    $string = "Placido P. Octopus";
+    $regex  = "P.";
 
-    $string =~ s/\Q$lhs/$rhs/;
-    # $string is now "to sleep no more"
+    $string =~ s/$regex/Polyp/;
+    # $string is now "Polypacido P. Octopus"
 
-Without the \Q, the regex would also spuriously match "di".
+Because C<.> is special in regular expressions, and can match any
+single character, the regex C<P.> here has matched the <Pl> in the
+original string.
+
+To escape the special meaning of C<.>, we use C<\Q>:
+
+    $string = "Placido P. Octopus";
+    $regex  = "P.";
+
+    $string =~ s/\Q$regex/Polyp/;
+    # $string is now "Placido Polyp Octopus"
+
+The use of C<\Q> causes the <.> in the regex to be treated as a
+regular character, so that C<P.> matches a C<P> followed by a dot.
 
 =head2 What is C</o> really for?
 
End of Patch.

Reply via email to