Thanks again almut. I understand and agree with what you are saying. I checked for the errors and this is what I got "error: Can't find string terminator '"' anywhere before EOF at (eval 5) line 1."

This made me realize that even though I might be quoting the arguments string in the code posted here correctly, I wasn't doing that right in the actual code where I also needed to first escape both $ in my hash to be \$\$myHash. Adding that extra stuff fixed it. I didn't even think that was the problem - there's a duh! moment. :)

While I am not sure why I wasn't getting the "Too many arguments..." error in my direct call to mySub("Hello world"), it is likely I would have got that error once I actually quoted my string right in the eval. I added the direct call in your code to highlight what I am saying about not getting the error when making the direct call:

my $code = << 'EOC'; sub mySub() { print "args: @_\n"; } EOC eval $code; print qq(\ncalling 'mySub()'\n); eval 'mySub()'; print "error: $@" if $@; print qq(\ncalling 'mySub("hello world")' directly\n); mySub("hello world"); print qq(\ncalling 'mySub("hello world")'\n); eval 'mySub("hello world")'; print "error: $@" if $@;

Running this returned:

calling 'mySub()' args: calling 'mySub("hello world")' directly args: hello world calling 'mySub("hello world")' error: Too many arguments for main::mySub at (eval 3) line 2, near ""h +ello world")"

In reply to Re^4: eval sub by samip
in thread eval sub by samip

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.