my $var = \&funcname;
&$var;
sub funcname { print "funcname\n" }
This is preferable to storing the subroutine name in a
variable
(symbolic reference):
$var = 'funcname';
&$var;
If you think you need the above construct,
you are probably mistaken. Symbolic references are
namespace hoodoo and should be
avoided unless you know what you are getting into
(which is why 'use strict' complains). If you
need to store function names in a variable, use a
lookup hash with hard references.
my %dispatch = (foo => \&foo,
bar => \&bar );
foreach my $func (keys %dispatch) {
&{ $dispatch{$func} };
}
sub foo {
print "foo\n";
}
sub bar {
print "bar\n";
}
perlman:perlref describes references in detail.
The 5.6.0 perlfaq7 also has a bunch of information about
this under the question
"How can I use a variable as a variable name?"
Update: There's also this thread.
|