in reply to Re: eval problem
in thread eval problem
Thanks. I don't understand this error:
Modification of a read-only value
I don't see how I'm trying to modify $1.
You should have checked if the eval succeeded.
I read the docs on eval(), and I have no idea what it does. I know what eval does in other languages, but I couldn't get a single example using eval to work in perl. The examples in the docs are really poor.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: eval problem
by ikegami (Patriarch) on Dec 03, 2009 at 21:31 UTC | |
Your evaluating --$1, where -- is the predecrement operator. Seems pretty straight forward to me
As for errors,
The difference is that you can build the code dynamically
Be careful about using external inputs:
| [reply] [d/l] [select] |
by 7stud (Deacon) on Dec 04, 2009 at 01:25 UTC | |
Your evaluating --$1, where -- is the predecrement operator. Ah. That was a bad choice of characters in my example. Also, there is nothing in the eval docs to indicate why this doesn't work:
In other words, why are double double quotes required? Shouldn't the q{} operator return whatever is inside it without interpolation? Wouldn't that be "$temp"? And when "$temp" is eval()'ed wouldn't the qq{} operator around $temp return a string with the $variables interpolated? Or, is it that eval() only does one pass of interpolation over "$temp" leaving me with: my $repl = "xxx$1";and therefore a first pass of interpolation is necessary with qq{} to present eval with "xxx$1"? Seems pretty straight forward to me For eval EXPR, the return value of EXPR is parsed and executed as if it were a little Perl program. The value of the expression (which is itself determined within scalar context) is first parsed, and if there weren't any errors, executed in the lexical context of the current Perl program. Let's test out this eval EXPR thing. First, I need an expression that returns a perl statement that can then be "executed as if it were a little program". The following code seems to produce an expression that I can use:
The return value of the sprintf expression is the string that is displayed. Ok, now I'm going to eval that expression. According to the docs you quoted, the value of the expression will be "executed in the lexical content of the current Perl program"--whatever that means. I'll try using eval() and see what happens:
I thought I followed the instructions in the docs to the letter, but eval EXPR just spits out errors. What about the block form of eval(): eval BLOCK? The docs say: the BLOCK is parsed only once‐‐at the same time the code surrounding the "eval" itself was parsed‐‐and executed within the context of the current Perl program. This seems much easier; no sprintf necessary:
The heck with the descriptions, I'll examine a couple of examples in the docs and figure it out by myself. Hmmm...none of the examples in the docs will work either because they are incomplete. So...no. The docs do not seem straightforward to me. | [reply] [d/l] [select] |
by ikegami (Patriarch) on Dec 04, 2009 at 02:18 UTC | |
Yes. The only special sequences in single quotes are: backslash-backslash, backslash-delimiter and delimiter.
Yes
Yes. The value of $temp is interpolated.
I don't know why you used sprintf in any of the examples. You do realize that is just as much an expression as
right? An expression is a piece of code that can be used as an operand. That includes simple values such as a string.
Not true. eval EXPR doesn't spit out errors. It places them in $@ and you didn't even check $@. The message you posted came from say $y;. What you're missing is that Perl programs are compiled and executed in their own scope, usually called "file scope". Think of the eval code as being inside a do{}. Yeah, that could be made more explicit.
Don't try to apply the docs for eval EXPR to eval BLOCK at all. It's a completely different operator. In Java, C++ and Perl6, it's known as try. It won't help you at all here. | [reply] [d/l] [select] |
by ikegami (Patriarch) on Dec 04, 2009 at 04:57 UTC | |
I missed this question of yours originally
eval EXPR doesn't interpolate. It parses and executes the Perl code it is provided. As for Perl, it doesn't interpolate recursively. Nothing I know does. (Perl, bash, Template-Toolkit, ...) quotemeta would become the most used function if it did. | [reply] [d/l] [select] |