Shouldn't the q{} operator return whatever is inside it without interpolation?
Yes. The only special sequences in single quotes are: backslash-backslash, backslash-delimiter and delimiter.
Wouldn't that be "$temp"?
Yes
$ perl -le'print q{"$temp"}' "$temp"
And when "$temp" is eval()'ed wouldn't the qq{} operator around $temp return a string with the $variables interpolated?
Yes. The value of $temp is interpolated.
$ perl -le'$temp=q{Bread was $1.50}; print eval q{"$temp"}' Bread was $1.50 $ perl -le'$temp=q{Bread was $1.50}; print "$temp"' Bread was $1.50
This seems much easier; no sprintf necessary:
I don't know why you used sprintf in any of the examples. You do realize that
is just as much an expression as'...'
sprintf('%s', '...')
right? An expression is a piece of code that can be used as an operand. That includes simple values such as a string.
I thought I followed the instructions in the docs to the letter, but eval EXPR just spits out errors.
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.
$ perl -E'use strict; my $x = 10; eval q{ my $y = $x; }; say $y' Global symbol "$y" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $ perl -E'use strict; my $x = 10; eval q{ my $y = $x; say $y };' 10 $ perl -E'use strict; my $x = 10; my $y; eval q{ $y = $x; }; say $y' 10 $ perl -E'use strict; my $x = 10; my $y = eval q{ $x }; say $y' 10
eval {my $y = $x};
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.
In reply to Re^5: eval problem
by ikegami
in thread eval problem
by 7stud
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |