I've been fiddling with trying to resolve scalar values within a string. I've been digging around for a bit and found the eval. If I understand eval right, it's not really meant for what I want or I'm misusing it in some fashion :(

For example, the following code has expected behavior where each scalar value in a string is replaced with the appropriate value.

my $alpha = "A"; my $delta = "D"; my $test = "$delta and $alpha"; print "$test";
Will naturally print the following string;
D and A

Now the following code will behave exactly as I expect it to.

my $alpha = "A"; my $delta = "D"; my $test = '$delta and $alpha'; print "$test";
Of course, it will print
$delta and $alpha
No surprise there.

But what if I want it to evaluate the contents in that scalar as string expression? So I read into eval and I understand it to be meant to use to evaulate actual blocks of code. As in if an Eval block is just a regular block of code. So it returns all the same things a regular block would do. But according to my llama book, it states that it also has the advantage that it protects the primary source from errors. Especially if you're calling third party code and you don't have any assurance that it won't fail gracefully.

So now, I've run out of choice search phrases to try. Any suggestions?


In reply to Resolving scalars in a scalar by Anonymous Monk

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.