@@ -593,17 +593,15 @@ say %h.perl
593
593
594
594
= head2 C « <{$x}> » vs C « $($x) » : Implicit EVAL
595
595
596
- Sometimes you may need to match a generated string in a regex. The
597
- right way to do it is to use C < $(…) > or C « <{…}> » syntax.
596
+ Sometimes you may need to match a generated string in a regex. This can be done
597
+ using C < $(…) > or C « <{…}> » syntax:
598
598
599
599
= for code
600
600
my $x = ‘ailemac’;
601
601
say ‘I ♥ camelia’ ~~ / $($x.flip) /; # OUTPUT: «「camelia」»
602
- my $x = ‘ailemac’;
603
602
say ‘I ♥ camelia’ ~~ / <{$x.flip}> /; # OUTPUT: «「camelia」»
604
603
605
- However, there is a wrong way to write it, and the problem is that
606
- it works I < sometimes > .
604
+ However, the latter only works I < sometimes > .
607
605
608
606
Internally C « <{…}> » EVAL-s the given string inside an anonymous regex, while
609
607
C < $(…) > lexically interpolates the given string. So C « <{…}> » immediately breaks
@@ -613,7 +611,7 @@ with more complicated inputs. For example:
613
611
my $x = ‘ailemac#’;
614
612
say ‘I ♥ #camelia’ ~~ / $($x.flip) /; # OUTPUT: «「#camelia」»
615
613
# ⚠ ↓↓ WRONG ↓↓ ⚠
616
- say ‘I ♥ camelia’ ~~ / <{$x.flip}> /; # OUTPUT: «「camelia」»
614
+ say ‘I ♥ # camelia’ ~~ / <{$x.flip}> /;
617
615
# OUTPUT:
618
616
# ===SORRY!===
619
617
# Regex not terminated.
@@ -639,13 +637,13 @@ thrown, and should not introduce security issues.
639
637
To match one of several possible alternatives, C < || > or C < | > will be used. But
640
638
they are so different.
641
639
642
- While there are more alternations than one matched , for those separated by
640
+ When there are multiple matching alternations , for those separated by
643
641
C < || > , the first matching alternation wins; for those separated by C < | > ,
644
642
which to win is decided by LTM strategy. See also:
645
643
L < documentation on C < || > |/language/regexes#Alternation:_|| > and
646
644
L < documentation on C < | > |/language/regexes#Longest_Alternation:_| > .
647
645
648
- For advice of which to use: for simple regexes just using C < || > instead of C < | >
646
+ For simple regexes just using C < || > instead of C < | >
649
647
will get you familiar semantics, but if writing grammars then it's useful to
650
648
learn about LTM and declarative prefixes and prefer C < | > . And keep yourself
651
649
away from using them in one regex. When you have to do that, add parentheses
0 commit comments