@@ -921,29 +921,25 @@ of the assignment operators instead:
921
921
922
922
= head1 Regexes
923
923
924
- = head1 Interpolation constructs
924
+ = head2 C « $x » vs C « <$x> » , and C « $(code) » vs C « <{code}> »
925
925
926
926
Perl 6 offers several constructs to generate regexes at runtime through
927
927
interpolation (see their detailed description
928
- L < here|/language/regexes#Regex_interpolation > ). When a thus generated regex
929
- contains only characters that match themselves, some of these constructs behave
930
- identically, as if they are equivalent alternatives. As soon as the generated
931
- regex contains metacharacters, however, they behave differently, which may come
932
- as an unpleasant and confusing surprise.
928
+ L < here|/language/regexes#Regex_interpolation > ). When a regex generated this way
929
+ contains only literals, the above constructs behave (pairwise) identically, as
930
+ if they are equivalent alternatives. As soon as the generated regex contains
931
+ metacharacters, however, they behave differently, which may come as a confusing
932
+ surprise.
933
933
934
934
The first two constructs that may easily be confused with each other are
935
- C « $variable » and C « <$variable> » . The former causes the (stringified) variable to
936
- match literally, while the latter causes the (stringified) variable to match as
937
- a regex. As long as the variable comprises only characters that, in a regex,
938
- match themselves (i.e. alphanumeric characters and the underscore), there is no
939
- distinction between the constructs:
935
+ C « $variable » and C « <$variable> » :
940
936
941
937
my $variable = 'camelia';
942
938
say ‘I ♥ camelia’ ~~ / $variable /; # OUTPUT: 「camelia」
943
939
say ‘I ♥ camelia’ ~~ / <$variable> /; # OUTPUT: 「camelia」
944
940
945
- But when the variable is changed to comprise regex metacharacters, i.e .
946
- characters that are neither alphanumeric nor the underscore C < _ > , the outputs
941
+ Here they act the same because the value of C < $variable > consists of literals .
942
+ But when the variable is changed to comprise regex metacharacters the outputs
947
943
become different:
948
944
949
945
my $variable = '#camelia';
@@ -957,12 +953,9 @@ runs until the end of the line, which in turn causes the regex not to be
957
953
terminated, and thus to be malformed.
958
954
959
955
Two other constructs that must similarly be distinguished from one another are
960
- C « $(code) » and C « <{code}> » . The former construct runs user-specified code within
961
- the regex and interpolates the (stringified) return value literally. The latter
962
- also runs user-specified code within the regex, but interpolates the
963
- (stringified) return value as a regex. So, like before, as long as the return
964
- value comprises only characters that match literally in a regex, there is no
965
- distinction between the two:
956
+ C « $(code) » and C « <{code}> » . Like before, as long as the (stringified) return
957
+ value of C < code > comprises only literals, there is no distinction between the
958
+ two:
966
959
967
960
my $variable = 'ailemac;
968
961
say ‘I ♥ camelia’ ~~ / $($variable.flip) /; # OUTPUT: 「camelia」
0 commit comments