Skip to content

Commit

Permalink
Deletion of regex-interpolation trap section
Browse files Browse the repository at this point in the history
  • Loading branch information
threadless-screw committed Aug 6, 2019
1 parent 55b8c01 commit 98f809b
Showing 1 changed file with 0 additions and 41 deletions.
41 changes: 0 additions & 41 deletions doc/Language/traps.pod6
Expand Up @@ -921,47 +921,6 @@ of the assignment operators instead:
=head1 Regexes
=head2 C«<{$x}>» vs C«$($x)»: Implicit EVAL
Sometimes you may need to match a generated string in a regex. This can be done
using C<$(…)> or C«<{…}>» syntax:
=for code
my $x = ‘ailemac’;
say ‘I ♥ camelia’ ~~ / $($x.flip) /; # OUTPUT: «「camelia」␤»
say ‘I ♥ camelia’ ~~ / <{$x.flip}> /; # OUTPUT: «「camelia」␤»
However, the latter only works I<sometimes>.
Internally C«<{…}>» EVAL-s the given string inside an anonymous regex, while
C<$(…)> lexically interpolates the given string. So C«<{…}>» immediately breaks
with more complicated inputs. For example:
=for code
my $x = ‘ailemac#’;
say ‘I ♥ #camelia’ ~~ / $($x.flip) /; # OUTPUT: «「#camelia」␤»
# ⚠ ↓↓ WRONG ↓↓ ⚠
say ‘I ♥ #camelia’ ~~ / <{$x.flip}> /;
# OUTPUT:
# ===SORRY!===
# Regex not terminated.
# at EVAL_0:1
# ------> anon regex { #camelia}⏏<EOL>
# Malformed regex
# at EVAL_0:1
# ------> anon regex { #camelia}⏏<EOL>
# expecting any of:
# infix stopper
Therefore, try not to use C«<{}>» unless you really need EVAL.
Note that even though EVAL is normally considered unsafe, in this case
it is restricted to a set of safe operations (which is why it works
without L<MONKEY-SEE-NO-EVAL|
/language/pragmas#index-entry-MONKEY-SEE-NO-EVAL-MONKEY-SEE-NO-EVAL> pragma).
In theory, careless use of C«<{}>» will only result in an exception being
thrown, and should not introduce security issues.
=head2 C<|> vs C<||>: which branch will win
To match one of several possible alternatives, C<||> or C<|> will be used. But
Expand Down

0 comments on commit 98f809b

Please sign in to comment.