Hi - I am attempting to EVAL a subroutine defined in my XML file and later call it in the Perl code (the call is in the XML file).

I am having problems wherein I can't seem to get it work if my sub has any quoted arguments, so I am assuming I am just not escaping or interpolating the "" right?

My XML contains a sub as follows: sub mySub() { # stuff }

My call mySub() to the sub works fine. However, if I try mySub("Hello World"), it does not return anything upon evaluation. If I hard code the direct calls in my Perl script it works either ways.

Also in the code the line below will cause an error to be thrown saying "Useless use of string in void context at test.pl line XX."

#eval { eval { "$config->{client}->{1}->{file}->{1}->{executeInternal} +" } };

However, if I replace the "{}" with "()" it doesn't throw that error. Could someone please explain why using the block form does this?

eval ( eval { "$config->{client}->{1}->{file}->{1}->{executeInternal} +" } );

Below is the code (Perl and XML) that I am attempting to use:

Perl code:

#!/usr/bin/perl -w use warnings; use strict; use XML::Simple; # Getting the ref using XMLIn my $config = XMLin("test.xml", forceArray=>[qw(config client file)], s +uppressEmpty=>undef); # Print the actual subroutine and call from XML print "The actual code in the XML is:\n"; print $config->{client}->{1}->{file}->{1}->{subroutines}->{callInterna +lSub},"\n"; # Eval the sub - works fine - contains a mySub() call without argument +s print "EVALing the actual code in the XML. Works fine if my call is si +mply mySub() without arguments. \n"; eval (qq"{" . $config->{client}->{1}->{file}->{1}->{subroutines}->{cal +lInternalSub} . "}"); print "\n"; #Direct call to EVALed sub - works fine print "Direct call to the EVALed sub without arguments works fine. \n" +; mySub("hello world"); print "\n"; # Print a bunch of stuff print "Print a bunch of stuff:\n"; print "$config->{client}->{1}->{file}->{1}->{executeInternal}\n"; print eval { "$config->{client}->{1}->{file}->{1}->{executeInternal}" +},"\n"; print eval qq{"$config->{client}->{1}->{file}->{1}->{executeInternal}" +}; print "\n\nNow trying the actual eval:\n"; eval (qq"{" . $config->{client}->{1}->{file}->{1}->{executeInternal} . + "}"); # As expected, the line below works too (not because of the EVAL) - #eval (qq"{" . mySub("hello world") . "}"); # Do I have to escape my "" in my XML file? If I simple call mySub() i +n the XML, it works fine. # I tried a few things (\,\\) to no avail. # The line below will cause an error to be thrown saying "Useless use +of string in void context at test.pl line XX." #eval { eval { "$config->{client}->{1}->{file}->{1}->{executeInternal} +" } }; # However, if I replace the "{}" with "()" it doesn't throw that error +. Could someone please explain why using the # block form does this? #eval ( eval { "$config->{client}->{1}->{file}->{1}->{executeInternal} +" } );

XML code:

<config> <client name="1"> <file name="1"> <executeInternal>mySub(\"Hello World\")</executeInternal> <executeInternalWORKS>mySub()</executeInternalWORKS> <subroutines> <callInternalSub> sub mySub() { my $message = "blah"; my $message = shift; #broken version print "Message- $message\n"; print "Caller-> ",caller,"\n"; } mySub(); </callInternalSub> </subroutines> </file> </client> </config>

I apologize if the comments in the code are excessive. Thank you for your help.

In reply to 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.