X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq6.pod;h=168233bd1b569c5f29ee8f91246e312f83212834;hb=363c40c40eaf5d0cfd92f460a3f838c41f9756ad;hp=9bbf80a0189fe870dd89bd7f1e346f93b06e715a;hpb=197aec242db45fbf1d7853a1ae22a108cc09d23c;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq6.pod b/pod/perlfaq6.pod index 9bbf80a..168233b 100644 --- a/pod/perlfaq6.pod +++ b/pod/perlfaq6.pod @@ -292,14 +292,26 @@ a double-quoted string (see L for more details). Remember 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 here has matched the 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 matches a C

followed by a dot. =head2 What is C really for? @@ -667,15 +679,18 @@ guaranteed is slowness.) See the book "Mastering Regular Expressions" hope to know on these matters (a full citation appears in L). -=head2 What's wrong with using grep or map in a void context? +=head2 What's wrong with using grep in a void context? -The problem is that both grep and map build a return list, -regardless of the context. This means you're making Perl go -to the trouble of building a list that you then just throw away. -If the list is large, you waste both time and space. If your -intent is to iterate over the list then use a for loop for this +The problem is that grep builds a return list, regardless of the context. +This means you're making Perl go to the trouble of building a list that +you then just throw away. If the list is large, you waste both time and space. +If your intent is to iterate over the list, then use a for loop for this purpose. +In perls older than 5.8.1, map suffers from this problem as well. +But since 5.8.1, this has been fixed, and map is context aware - in void +context, no lists are constructed. + =head2 How can I match strings with multibyte characters? Starting from Perl 5.6 Perl has had some level of multibyte character