Indirect function call via Name



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/

.



Relevant Pages

  • Re: Looking for a slick way to classify relationship between two numbers, without tons of if/else
    ... Before one can "optimize" a function, one needs to get the function correct. ... And if a is positive and b is None, it'll return None, rather than any string. ... That brings us to the question of what is more elegant. ... To me the elegant code is readable first, and efficient second. ...
    (comp.lang.python)
  • Re: chop, separate, split a STRING into sections?
    ... code looping such as DO WHILE or FOR NEXT. ... Public Function Chunk(ByVal str As String, ByVal chunkSize As Integer) As String ... an "elegant" solution. ... While first off, I don't see the problem with mid, substring, or looping:) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Finding 1 of 3 different strings in a string
    ... I doubt if anyone would call this "elegant", but it should do what you want... ... Note the absolute cell addresses used in the example formulas being executed inside the CHOOSE function in response to your 3 error code messages... ... It would nice if Findreturned 0 if the string was ...
    (microsoft.public.excel.worksheet.functions)
  • Re: chop, separate, split a STRING into sections?
    ... Here's a sample of what I'm looking for: break apart a string into ... and there's no 'single function' or elegant 'one- ... Public Function Chop(ByVal sIn As String, ByVal iCars As Integer) As List(Of ... Dim los As New List ...
    (microsoft.public.dotnet.languages.vb)
  • Re: chop, separate, split a STRING into sections?
    ... Here's a sample of what I'm looking for: break apart a string into ... and there's no 'single function' or elegant 'one- ... What about converting the string to a Character Array then using Group ...
    (microsoft.public.dotnet.languages.vb)