Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit EVAL in regexes (trap?) #1429

Closed
AlexDaniel opened this issue Jul 29, 2017 · 4 comments
Closed

Implicit EVAL in regexes (trap?) #1429

AlexDaniel opened this issue Jul 29, 2017 · 4 comments
Labels
docs Documentation issue (primary issue type) trap

Comments

@AlexDaniel
Copy link
Member

Let's say you have this code:

my $x = hello;
say hello world ~~ /<{$x}>/

Result:

「hello」

So far everything looks alright. However, let's change the input data ($x) a little bit:

my $x = #hello;
say hello world ~~ /<{$x}>/

Result:

===SORRY!===
Regex not terminated.
at /home/alex/EVAL_0:1
------> anon regex { #hello}⏏<EOL>
Regex not terminated.
at /home/alex/EVAL_0:1
------> anon regex { #hello}⏏<EOL>
Malformed regex
at /home/alex/EVAL_0:1
------> anon regex { #hello}⏏<EOL>
    expecting any of:
        infix stopper

So it turns out you cannot use variables with arbitrary data in <{ }>? To be honest, I'm not entirely sure what is happening here and if it should work like this. For example, if we're in EVAL, then why MONKEY-SEE-NO-EVAL plays no role here? Would be nice to have this explained somewhere.

@AlexDaniel AlexDaniel added the docs Documentation issue (primary issue type) label Jul 29, 2017
@AlexDaniel
Copy link
Member Author

Oh, actually, MONKEY-SEE-NO-EVAL does work here, but in a weird way:

Code:

my $x = Hello{say ‘boo!’};
say #hello world ~~ / <{$x}> /

Result:

===SORRY!=== Error while compiling /home/alex/EVAL_0
Prohibited regex interpolation (use the MONKEY-SEE-NO-EVAL pragma to override this error,
but only if you're VERY sure your data contains no injection attacks)
at /home/alex/EVAL_0:1
------> anon regex { Hello⏏{say ‘boo!’}}

@AlexDaniel
Copy link
Member Author

And also, let's say I want to generate some string and match against it, is it possible to do it without creating a temp variable? For example, let's say I have $x but I want to use $x.lc inside a regex, or $x // ‘blah’ or whatever.

So far I came up with this:

my $x = #Hello;
say #hello world ~~ / <{ my $foo = $x.lc; /$foo/ }> /

Maybe it works, I'm not entirely sure. But that's ridiculous anyway. What's the proper way to do it?

@jnthn
Copy link
Contributor

jnthn commented Jul 29, 2017

/$($x.lc)/

@AlexDaniel
Copy link
Member Author

Yup! This is it. Not sure if I've ever seen it, but definitely expected something like this. Cool, thanks.

AlexDaniel added a commit to Raku/whateverable that referenced this issue Aug 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation issue (primary issue type) trap
Projects
None yet
Development

No branches or pull requests

2 participants