@@ -1350,24 +1350,60 @@ 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".)
1354+
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+ But a non-alphanumeric will always match literally when preceded by a
1368+ backslash. Hence simply adding backslashes before all non-"word"
1369+ characters can be used to disable the special meanings of regular
1370+ expression metacharacters in a string that you want to use for a
1371+ pattern.
13621372
13631373 $pattern =~ s/(\W)/\\$1/g;
13641374
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:
1375+ then
13691376
1370- /$unquoted\Q$quoted\E$unquoted/
1377+ $string =~ s/$pattern/foo/;
1378+
1379+ (If C<use locale> is in effect, the current locale can affect the
1380+ results.)
1381+
1382+ This template used to be a common paradigm, but these days it is more
1383+ usual to use the C<L<quotemeta()|perlfunc/quotemeta>>
1384+ function or especially the C<\Q> metaquoting escape sequence to disable all
1385+ metacharacters' special meanings.
1386+
1387+ In fact, C<quotemeta> effectively does the same thing as the old
1388+ paradigm, but without any locale dependence. That is,
1389+
1390+ quotemeta $pattern;
1391+
1392+ does either of these:
1393+
1394+ $pattern =~ s/(\W)/\\$1/ug;
1395+ $pattern =~ s/(\W)/\\$1/ag;
1396+
1397+ The first statement applies if the
1398+ L<< C<unicode_strings>|feature/"The 'unicode_strings' feature" >> is
1399+ enabled; the second if that feature is disabled.
1400+
1401+ S<C<\Q>...C<\E>> acts identically, but you don't have to have a separate
1402+ scalar to hold C<$pattern>, and it has the significant added flexibility
1403+ to allow you to selectively apply it to any portions of the matched
1404+ string you choose; like this:
1405+
1406+ $string =~ s/$unquoted\Q$quoted\E$unquoted/foo/;
13711407
13721408Beware that if you put literal backslashes (those not inside
13731409interpolated variables) between C<\Q> and C<\E>, double-quotish
0 commit comments