diff options
author | Shlomi Fish <shlomif@iglu.org.il> | 2009-12-30 12:18:23 +0100 |
---|---|---|
committer | Abigail <abigail@abigail.be> | 2009-12-30 12:18:23 +0100 |
commit | c088ef569318bbdc082566d3567bbf231adc5267 (patch) | |
tree | b2b713418920bd89e4c5d592ebe0afd6668c586e /pod/perlfunc.pod | |
parent | 4d4acfba162f595158f37b543a161d98e61c1235 (diff) | |
download | perl-c088ef569318bbdc082566d3567bbf231adc5267.tar.gz |
Add a usage scenario and common use pattern to perldoc -f quotemeta
Diffstat (limited to 'pod/perlfunc.pod')
-rw-r--r-- | pod/perlfunc.pod | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 25e28e1b39..114d4dac9b 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -4328,6 +4328,32 @@ the C<\Q> escape in double-quoted strings. If EXPR is omitted, uses C<$_>. +quotemeta (and C<\Q> ... C<\E>) are useful when interpolating strings into +regular expressions, because by default an interpolated variable will be +considered a mini-regular expression. For example: + + my $sentence = 'The quick brown fox jumped over the lazy dog'; + my $substring = 'quick.*?fox'; + $sentence =~ s{$substring}{big bad wolf}; + +Will cause C<$sentence> to become C<'The big bad wolf jumped over...'>. + +On the other hand: + + my $sentence = 'The quick brown fox jumped over the lazy dog'; + my $substring = 'quick.*?fox'; + $sentence =~ s{\Q$substring\E}{big bad wolf}; + +Or: + + my $sentence = 'The quick brown fox jumped over the lazy dog'; + my $substring = 'quick.*?fox'; + my $quoted_substring = quotemeta($substring); + $sentence =~ s{$quoted_substring}{big bad wolf}; + +Will both leave the sentence as is. Normally, when accepting string input from +the user, quotemeta() or C<\Q> must be used. + =item rand EXPR X<rand> X<random> |