You can call a Perl function using a variable. For example below code, here the variable $perl_function_name whose value is "call_function", you can call the function as &$perl_function_name() instead of calling call_function();
For more Perl you can take a look at here http://icfun.blogspot.com/search/label/perl
my $perl_function_name = "call_function";
## calling without parameter passing
&$perl_function_name();
## calling with parameter passing
&$perl_function_name(10);
## example function
sub call_function{
my $param = shift;
if($param){
print "Calling function using variable with parameter $param\n";
}
else{
print "Calling function using variable\n";
}
}
Using the 'use strict;' I found the code doesn't work. The error i got Can't use string ("call_function") as a subroutine ref while "strict refs" in use Anyone knows why? Or any other way to do this?For more Perl you can take a look at here http://icfun.blogspot.com/search/label/perl
Comments
use strict;
no strict "refs";
Check out the docs, though, as this does have other effects.
eval{ &$perl_function_name(10)} ;
This will work only in run time, and you wouldn't need to use
no strict "refs";
{
no strict "refs";
$obj->$name(...);
}