@@ -1350,25 +1350,36 @@ X</p> X<p modifier>
1350
1350
1351
1351
=head2 Quoting metacharacters
1352
1352
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.
1362
1368
1363
1369
$pattern =~ s/(\W)/\\$1/g;
1364
1370
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>>
1367
1375
function or the C<\Q> metaquoting escape sequence to disable all
1368
1376
metacharacters' special meanings like this:
1369
1377
1370
1378
/$unquoted\Q$quoted\E$unquoted/
1371
1379
1380
+ (These are not locale-dependent, so won't necessarily give the same
1381
+ results as when under S<C<use locale>>.)
1382
+
1372
1383
Beware that if you put literal backslashes (those not inside
1373
1384
interpolated variables) between C<\Q> and C<\E>, double-quotish
1374
1385
backslash interpolation may lead to confusing results. If you
0 commit comments