@@ -1350,25 +1350,36 @@ 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+ To cause a metacharacter to match its literal self, you quote (or
1354+ escape) it by preceding it with a backslash. Unlike some other
1355+ regular expression languages, any sequence consisting of a backslash
1356+ followed by a non-alphanumeric matches that non-alphanumeric, literally.
1357+ So things like C<\\>, C<\(>, C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> are
1358+ always interpreted as the literal character that follows the backslash.
1359+
1360+ (That's not true when an alphanumeric character is preceded by a
1361+ backslash. There are a few such sequences, like C<\w>, which have
1362+ special meaning for Perl.)
1363+
1364+ But with a non-alphanumeric, it will match literally. Hence simply
1365+ quoting all non-"word" characters can be used to disable the special
1366+ meanings of regular expression metacharacters in a string that you want
1367+ to use for a pattern.
13621368
13631369 $pattern =~ s/(\W)/\\$1/g;
13641370
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>>
1371+ (If C<use locale> is in effect, then this depends on the current locale.)
1372+
1373+ This template used to be a common paradigm, but these days it is more
1374+ usual to use the C<L<quotemeta()|perlfunc/quotemeta>>
13671375function or the C<\Q> metaquoting escape sequence to disable all
13681376metacharacters' special meanings like this:
13691377
13701378 /$unquoted\Q$quoted\E$unquoted/
13711379
1380+ (These are not locale-dependent, so won't necessarily give the same
1381+ results as when under S<C<use locale>>.)
1382+
13721383Beware that if you put literal backslashes (those not inside
13731384interpolated variables) between C<\Q> and C<\E>, double-quotish
13741385backslash interpolation may lead to confusing results. If you
0 commit comments