I have a question on Perl syntax. Given a couple of functions:
#########
sub Test1
{
print "Test1\n";
}
sub Test2
{
print "Test2\n";
}
#######
How can I call the subroutine that is named in a scalar variable
$func?
Here, the scalar $func will contain either "Test1" or "Test2"
e.g. this doesn't work:
&($func)
Thanks,
Neil
Typically, uses are as a callback or dispatch table.
Each require the function address or reference to be
generically passed in and stored in another variable.
OR, knowing the KEY or INDEX where a particular handler's
function pointer or reference is stored.
In the old days for instance, packet handler adresses
in C were stored in an array of function pointers.
The packet was put together with the enum of the index into
this array imbedded into an inner layer, then sent down the pipe
where the reciever extracted the data layer containing the index,
then passed the data to the handler.
Still though, there is always a resolution to a fixed VARIABLE containing
the function address or reference. This instead of a symbolic reference.
Callbacks and handlers are a way for the caller to set a user defined function
that the callee calls at a later time in responce to some trigger. This can
be set any time but typically called sometime later.
Using methods as callbacks are a little trickier and may need wrappers.
In Perl, the caller needs to identify the handler (if many are used) to
the callee. The identifier can be an exported (if the callee is a module) constant
or a disposable key name, either of which are passed as an identifier, along with
the function reference and/or other data to the callee who either calls the function
immediatly or stores the reference into a callee provided variable, either hash or
scalar array.
At this point the callee can do basic checks like ref($val) eq 'CODE' or such as
that.
Anyway, the use of & when calling a function allows the callee to see the callers
@_ when dispatching functions and is usually used as a pass through, for example,
when called from the replacement side of a s///e regex.
Anyway, here are some examples. Perlsub should give more info. But in general,
symbolic reference is bad.
-sln
----------------------------
use warnings;
use strict;
sub Test1
{
print "@_ Test1\n";
}
sub Test2
{
print "@_ Test2\n";
}
my %funcref = (
Test1 => \&Test1,
Test2 => \&Test2,
Test3 => sub {print "@_ Test3 anonymous sub\n";},
Test4 => sub {print "@_ Test4 anonymous sub\n";},
);
@_ = ('some', 'stuff');
for my $code (sort keys %funcref)
{
print ".. $code\n";
my $fn = $funcref { $code };
$fn->();
&$fn;
$funcref{ $code }();
&{$funcref{ $code }};
$funcref{ $code }->();
print "\t---\n";
}
__END__
... Test1
Test1
some stuff Test1
Test1
some stuff Test1
Test1
---
... Test2
Test2
some stuff Test2
Test2
some stuff Test2
Test2
---
... Test3
Test3 anonymous sub
some stuff Test3 anonymous sub
Test3 anonymous sub
some stuff Test3 anonymous sub
Test3 anonymous sub
---
... Test4
Test4 anonymous sub
some stuff Test4 anonymous sub
Test4 anonymous sub
some stuff Test4 anonymous sub
Test4 anonymous sub
---