@@ -1350,32 +1350,61 @@ X</p> X<p modifier>
13501350
13511351=head2 Quoting metacharacters
13521352
1353- Backslashed metacharacters in Perl are alphanumeric, such as C<\b>,
1354- C<\w>, C<\n>. Unlike some other regular expression languages, there
1355- are no backslashed symbols that aren't alphanumeric. So anything
1356- that looks like C<\\>, C<\(>, C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> is
1357- always
1358- interpreted as a literal character, not a metacharacter. This was
1359- once used in a common idiom to disable or quote the special meanings
1360- of regular expression metacharacters in a string that you want to
1361- use for a pattern. Simply quote all non-"word" characters:
1353+ (Also known as "escaping".)
13621354
1363- $pattern =~ s/(\W)/\\$1/g;
1355+ To cause a metacharacter to match its literal self, you precede it with
1356+ a backslash. Unlike some other regular expression languages, any
1357+ sequence consisting of a backslash followed by a non-alphanumeric
1358+ matches that non-alphanumeric, literally. So things like C<\\>, C<\(>,
1359+ C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> are always interpreted as the
1360+ literal character that follows the backslash.
1361+
1362+ (That's not true when an alphanumeric character is preceded by a
1363+ backslash. There are a few such "escape sequences", like C<\w>, which have
1364+ special matching behaviors in Perl. All such are currently limited to
1365+ ASCII-range alphanumerics.)
1366+
1367+ The best method to escape metacharacters is to use the
1368+ C<L<quotemeta()|perlfunc/quotemeta>> function, or the equivalent, but the
1369+ more flexible, and often more convenient, C<\Q> metaquoting escape
1370+ sequence
1371+
1372+ quotemeta $pattern;
1373+
1374+ This changes C<$pattern> so that the metacharacters are quoted. You can
1375+ then do
1376+
1377+ $string =~ s/$pattern/foo/;
13641378
1365- (If C<use locale> is set, then this depends on the current locale.)
1366- Today it is more common to use the C<L<quotemeta()|perlfunc/quotemeta>>
1367- function or the C<\Q> metaquoting escape sequence to disable all
1368- metacharacters' special meanings like this:
1379+ and be assured that any metacharacters in C<$pattern> will match their
1380+ literal selves. If you instead use C<\Q>, like:
13691381
1370- /$unquoted\Q$quoted\E$unquoted/
1382+ $string =~ s/\Qpattern/foo/;
1383+
1384+ you don't have to have a separate C<$pattern> variable. Further, there
1385+ is an additional escape sequence, C<\E> that can be combined with C<\Q>
1386+ to allow you to escape whatever portions of the pattern you desire:
1387+
1388+ $string =~ s/$unquoted\Q$quoted\E$unquoted/foo/;
13711389
13721390Beware that if you put literal backslashes (those not inside
13731391interpolated variables) between C<\Q> and C<\E>, double-quotish
13741392backslash interpolation may lead to confusing results. If you
13751393I<need> to use literal backslashes within C<\Q...\E>,
13761394consult L<perlop/"Gory details of parsing quoted constructs">.
13771395
1378- C<quotemeta()> and C<\Q> are fully described in L<perlfunc/quotemeta>.
1396+ In older code, you may see something like this:
1397+
1398+ $pattern =~ s/(\W)/\\$1/g;
1399+ $string =~ s/$pattern/foo/;
1400+
1401+ This simply adds backslashes before all non-"word" characters to disable
1402+ any special meanings they might have. (If S<C<use locale>> is in
1403+ effect, the current locale can affect the results.) This paradigm is
1404+ inadequate for Unicode.
1405+
1406+ C<quotemeta()> and C<\Q> are more fully described in
1407+ L<perlfunc/quotemeta>.
13791408
13801409=head2 Extended Patterns
13811410
0 commit comments