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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |