Might I recommend a different way to build up your string which may be a bit closer to what you're trying to accomplish? (I'm an advocate of making my solutions look like the problem, not like the solution.)

I'm assuming your "for loop" is using a list such as @args.

my $call = '$jobs->$func(' . join(', ', map qq{"$_"}, @args) . ')'; my %jobstats = eval $call;

I'm avoiding all the chops as I don't add more commas than needed. That said, even the way you're doing it, you could remove the chops - trailing commas don't bother perl at all. My guess is that perl would parse and discard the comma faster inside the compiler than by using chop anyway.

Anyway, this code builds your method call in exactly the same way that you normally do. You convert your arguments to be quoted (converting one list to another is a map), you put commas between them (putting a string - even if it were an empty string - repeatedly between other strings is a join), and you put stuff before and after (with the dot operator).

Using the escape (backslash) as has already been suggested is one way to do this when you need to use double quotes. But I find escapes to be distracting and like to avoid them when reasonably possible.

Belgarion is correct in what your problem is. I'm just offering another way to approach your basic problem that may prove better.

Note: Even better for this scenario is:

my %jobstats = $jobs->$func(@args);
There's no need, in this scenario, to use eval at all. But I'm presuming you have something that becomes a bit complex that you want to use eval for - such as @args having "$foo" as one of its values, and eval STRING will then see "$foo" and interpolate the $foo to be whatever $foo contains. Which is generally a bad idea when you can avoid it - use hashes.


In reply to Re: problem in using eval strings by Tanktalus
in thread problem in using eval strings by rsennat

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.