cvs commit: perlfaq perlfaq6.pod

[email protected] (Robert Spier) 20 Sep 2003 06:38:54 -0000
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
cvsuser     03/09/19 23:38:54

  Modified:    .        perlfaq6.pod
  Log:
  From: Mark Jason Dominus <[email protected]>
  To: [email protected]
  Cc: [email protected]
  Subject: [PATCH perlfaq6.pod] Explain \Q better
  Date: Mon, 01 Sep 2003 16:19:20 -0400
  
  The 'example' in perlfaq6 of the use of \Q is ineffective:
  
  Revision  Changes    Path
  1.21      +19 -7     perlfaq/perlfaq6.pod
  
  Index: perlfaq6.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq6.pod,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -w -r1.20 -r1.21
  --- perlfaq6.pod	3 Jan 2003 20:05:28 -0000	1.20
  +++ perlfaq6.pod	20 Sep 2003 06:38:54 -0000	1.21
  @@ -1,6 +1,6 @@
   =head1 NAME
   
  -perlfaq6 - Regular Expressions ($Revision: 1.20 $, $Date: 2003/01/03 20:05:28 $)
  +perlfaq6 - Regular Expressions ($Revision: 1.21 $, $Date: 2003/09/20 06:38:54 $)
   
   =head1 DESCRIPTION
   
  @@ -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?