CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Eval and args

 



trysil
Novice

Feb 6, 2012, 12:44 AM

Post #1 of 2 (1983 views)
Eval and args Can't Post

Hi!
I am trying to implement the following:
With the help of XML::Simple I read in a xml file and from that file I get a
number of sub calls.


Code
my $xs = XML::Simple -> new(); 
my $ref = $xs -> XMLin();
my $call = $ref -> {fun};
my $arg = $ref -> {arg};
eval ($call);


But now I want to send a list of arguments (the $arg) to the called sub but I'm not sure how.

/T


rovf
Veteran

Feb 6, 2012, 4:28 AM

Post #2 of 2 (1976 views)
Re: [trysil] Eval and args [In reply to] Can't Post

From your code, I guess that the function is represented as a string, denoting its name. Right?

Here is the approach I would take:


Code
(sub {  my $fn = shift;  eval "&$fn"; })->($call,$arg);


I don't claim that this is the best solution, but it's the one which came to my mind when I read your question. It uses the special form &FUNCTIONNAME (i.e. no parenthesis), which passes @_ to the called function.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives