in reply to Variable Sub Calls
It printed "I am doing it". There is more than one way to do it:my $name = "do"; sub do { print "I am doing it\n"; } &$name;
Notice that since both methods use symbolic references, they fail under use strict;. Another approach is:my $name = "do"; sub do { print "I am doing this now\n"; } $name->();
That depends on being in the main package, however. It does work under use strict.$name = \& {"main::$name"}; &$name;
A better approach might be building a hash of references to subroutines:
Nicer, isn't it?sub foo { print "Doing foo (whatever that means)\n"; } sub bar { print "Now doing bar (isn't this fun?)\n"; } my %sub_table = ( 'foo' => \&foo, 'bar' => \&bar, ); foreach ( qw (foo bar foobar ) ) { $sub_table{$_}->(); }
|
|---|