in reply to eval sub

Could someone please explain why using the block form does this?

Because you combine block and string. What you have is equivalent to:

eval {"Hello!"}
All you're executing is a string in void context. You don't want a block here - the code you have is in a string, so you want to do string eval. Hence, you ought to be doing
eval $config->{client}->{1}->{file}->{1}->{executeInternal};

Replies are listed 'Best First'.
Re^2: eval sub
by samip (Novice) on Oct 09, 2008 at 20:25 UTC
    Thanks for the responses guys.

    moritz, thanks for correcting me - you are right, it is a warning, not an error - that was a typo that percolated as I was doing my copy/paste. I might not be understanding you correctly and am still unsure why I can't make a sub call in block form. I am pretty sure this would work: eval { mySub("hello world"); }
    eval <mystring> doesn't work when I have "" in the sub in the XML. I am unsure if I am not escaping/interpolating the "" correctly.

    My first thought was to try what javafan had and it just does not return anything upon evaluation. I then tried many different things to no avail. If I hard code the direct calls in my Perl script it works either ways.

    almut, thank you for your input. I don't believe prototyping to be an issue here. Don't you think the direct call should also not have worked in that case?

    Thanks again.
      I don't believe prototyping to be an issue here.

      Maybe.  What I meant is probably best expressed with a simple snippet of code, which resembles in essence what I understood you're trying to do (but without the XML and stuff):

      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")'\n); eval 'mySub("hello world")'; print "error: $@" if $@;

      Running this gives:

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

      As you can see, the second call (with arguments) fails with the prototype in place.  And it fails silently, unless you check $@.

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