perlmonkey forgot to mention a reference to a named subroutine:
This is preferable to storing the subroutine name in a variable (symbolic reference):my $var = \&funcname; &$var; sub funcname { print "funcname\n" }
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.$var = 'funcname'; &$var;
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?"my %dispatch = (foo => \&foo, bar => \&bar ); foreach my $func (keys %dispatch) { &{ $dispatch{$func} }; } sub foo { print "foo\n"; } sub bar { print "bar\n"; }
Update: There's also this thread.
In reply to Re: using variables as functions
by takshaka
in thread using variables as functions
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |