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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.