Skip to content

Commit ccf2245

Browse files
authored
Merge pull request #2940 from perl6/traps
Improvement of regex-interpolation trap section
2 parents fc4d52b + f6039af commit ccf2245

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

doc/Language/traps.pod6

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -921,29 +921,25 @@ of the assignment operators instead:
921921
922922
=head1 Regexes
923923
924-
=head1 Interpolation constructs
924+
=head2 C«$x» vs C«<$x>», and C«$(code)» vs C«<{code}>»
925925
926926
Perl 6 offers several constructs to generate regexes at runtime through
927927
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.
933933
934934
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>»:
940936
941937
my $variable = 'camelia';
942938
say ‘I ♥ camelia’ ~~ / $variable /; # OUTPUT: 「camelia」
943939
say ‘I ♥ camelia’ ~~ / <$variable> /; # OUTPUT: 「camelia」
944940
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
947943
become different:
948944
949945
my $variable = '#camelia';
@@ -957,12 +953,9 @@ runs until the end of the line, which in turn causes the regex not to be
957953
terminated, and thus to be malformed.
958954
959955
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:
966959
967960
my $variable = 'ailemac;
968961
say ‘I ♥ camelia’ ~~ / $($variable.flip) /; # OUTPUT: 「camelia」

0 commit comments

Comments
 (0)