Indirect function call via Name
- From: Ronald Fischer <ronaldf@xxxxxx>
- Date: Fri, 05 Jan 2007 11:28:32 +0100
What would be the best (most elegant) way to solve the following:
Suppose I have the name of a function (i.e. not a coderef) as a
string, and I would like to call this function, passing to it
all the parameters of the current function (i.e. @_). For example:
our $func="bar";
sub foo
{
# Of course the following would not work
&$func(@_);
}
foo('x',5); # This is supposed to call &bar('x',5)
The above would work if $func were a code ref, but it is a string.
I came to the following solution which works, but is ugly:
use Data::Dumper;
sub foo
{
local $Data::Dumper::Terse=1; # No varnames
local $Data::Dumper::Indent=0; # No newlines
eval("$func(".join(',',Dumper(@_)).')');
}
Could someone suggest a better alternative?
Ronald
--
Ronald Fischer <ronaldf@xxxxxx>
Posted via http://www.newsoffice.de/
.
- Follow-Ups:
- Re: Indirect function call via Name
- From: Mark Clements
- Re: Indirect function call via Name
- From: anno4000
- Re: Indirect function call via Name
- Prev by Date: Re: Problematic Perl code
- Next by Date: Re: Problematic Perl code
- Previous by thread: How can I use the password and username stored in cookie files to access the web automatically
- Next by thread: Re: Indirect function call via Name
- Index(es):
Relevant Pages
|