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? 

I tried evaling a string first, but it didn't work, so to be absolutely certain about what was happening I used sprintf. Now I understand that q{} is an operator that returns a string.


Not true. eval EXPR doesn't spit out errors. It places them in $@ and you didn't even check $@.

Jesus Christ, all these $ variables--I didn't even know there was such a thing. Now, I see that $@ is like a 'catch' to an eval BLOCK's 'try'.

What you're missing is that Perl programs are compiled and executed in their own scope, usually called "file scope".

Well, I would hope so. What else is there?
$ 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.

But the docs say:

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, so that any variable settings or subroutine and format definitions remain afterwards.

In my opinion, your examples prove that the bolded part is false. In actuality, eval EXPR acts like EXPR is evaluated inside a block, and the block is nested inside the main program. Therefore my variables do not survive the end of the block.

Don't try to apply the docs for eval EXPR to eval BLOCK at all.

I wasn't. I was trying to apply the eval BLOCK docs to eval BLOCK.

eval BLOCK is a completely different operator. In Java, C++ and Perl6, it's known as try.

Well, that explains the baffling behavior of eval BLOCK.
I missed this question of yours originally

Or, is it that eval() only does one pass of interpolation over "$temp"

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.

Yes, instead of this:

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"...

I should have said:

And when "$temp" is eval()'ed wouldn't the qq{} operator around $temp return a string with the $variables interpolated? Or, is it that the qq{} operator only does one pass of interpolation over "$temp" when "$temp" is eval()'ed.

=================

I'm sure you've gotten all the info you needed about eval, but if you use lookaheads, you could make this work without needing to specify captures (with parentheses) in your $pattern.

Very clever. Thanks.

============

ikegami's replies have pretty well covered this question, but for further discussion of template evaluation in a substitution replacement (which is what I take the OP to be about), see nodes printig with variables in text and simple regular expression and, with a bit of redundancy, s/// treat rhs as regex.

Thanks. The Template solution looks like a good alternative too.


In reply to Re: eval problem by 7stud
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.