@@ -1257,44 +1257,34 @@ documents.
1257
1257
1258
1258
= head1 X < Regex Interpolation|regex, Regex Interpolation >
1259
1259
1260
- If you want to build a regex using pattern given at the runtime, regex
1261
- interpolation is what you looking for.
1260
+ If you want to build a regex using a pattern given at runtime, regex
1261
+ interpolation is what you are looking for.
1262
1262
1263
- There are two ways you can interpolate a string into regex as a pattern,
1264
- which are quite similar to interpolate it into a double-quote string.
1265
- That is using C « <$pattern> » or C « <{$pattern.method}> » .
1263
+ There are four ways you can interpolate a string into regex as a pattern.
1264
+ That is using C « $pattern » , C « $($pattern) » , C « <$pattern> » or
1265
+ C « <{$pattern.method}> » .
1266
1266
1267
1267
my $text = 'camelia';
1268
- my $pattern = '\w+';
1269
- my $pattern_ = 'ailemac';
1270
- say $text ~~ / <$pattern> /; # OUTPUT: «「camelia」»
1271
- say $text ~~ / <{$pattern}> /; # OUTPUT: «「camelia」»
1272
- say $text ~~ / <{$pattern_.flip}> /; # OUTPUT: «「camelia」»
1273
- # say $text ~~ / <$pattern_.flip> /; !!Compile Error!!
1268
+ my $pattern0 = 'camelia';
1269
+ my $pattern1 = 'ailemac';
1270
+ my $pattern2 = '\w+';
1271
+
1272
+ say $text ~~ / $pattern0 /; # OUTPUT: «「camelia」»
1273
+ say $text ~~ / $($pattern0) /; # OUTPUT: «「camelia」»
1274
+ say $text ~~ / $($pattern1.flip) /; # OUTPUT: «「camelia」»
1275
+ say 'ailemacxflip' ~~ / $pattern1.flip /; # OUTPUT: «「ailemacxflip」»
1276
+ say '\w+' ~~ / $pattern2 /; # OUTPUT: «「\w+」»
1277
+ say '\w+' ~~ / $($pattern2) /; # OUTPUT: «「\w+」»
1278
+
1279
+ say $text ~~ / <{$pattern1.flip}> /; # OUTPUT: «「camelia」»
1280
+ # say $text ~~ / <$pattern1.flip> /; # !!Compile Error!!
1281
+ say $text ~~ / <$pattern2> /; # OUTPUT: «「camelia」»
1282
+ say $text ~~ / <{$pattern2}> /; # OUTPUT: «「camelia」»
1283
+
1284
+ Note that the first two syntax interpolate the string lexically, while
1285
+ C « <$pattern> » and C « <{$pattern.method}> » causes L « implicit EVAL|
1286
+ /language/traps#<{$x}>_vs_$($x):_Implicit_EVAL » , which is a known trap.
1274
1287
1275
- Note that this syntax causes L « implicit EVAL|
1276
- /language/traps#<{$x}>_vs_$($x):_Implicit_EVAL » , that is a known trap.
1277
-
1278
- When your pattern is a lexical string, there are two more syntax helping you
1279
- interpolate it.
1280
-
1281
- my $text = 'camelia';
1282
- my $string = 'camelia';
1283
- my $string_ = 'ailemac';
1284
- say $text ~~ / $string /; # OUTPUT: «「camelia」»
1285
- say $text ~~ / $($string) /; # OUTPUT: «「camelia」»
1286
- say $text ~~ / $($string_.flip) /; # OUTPUT: «「camelia」»
1287
- say $text ~~ / $string_.flip /; # OUTPUT: «Nil»
1288
- say 'ailemacxflip' ~~ / $string_.flip /; # OUTPUT: «「ailemacxflip」»
1289
-
1290
- Because it interpolates a string lexically, it doesn't EVAL the string
1291
- implicitly.
1292
-
1293
- my $text = '42';
1294
- my $text_ = '\d+';
1295
- my $lexical = '\d+';
1296
- say $text ~~ / $($lexical) /; # OUTPUT: «Nil»
1297
- say $text_ ~~ / $($lexical) /; # OUTPUT: «「\d+」»
1298
1288
1299
1289
= head1 Adverbs
1300
1290
0 commit comments