in reply to call sub_function easily
Hello dideod.yang,
You can make your first foreach loop work with eval:
foreach my $array (@array) { eval "$array->()"; }
However, for reasons outlined in How can I use a variable as a variable name?, this is usually a bad idea. A better approach is to build a lookup table using a hash:
my %test_functions = ( test => \&test, test2 => \&test2, test3 => \&test3, ); $test_functions{$_}->() for qw/ test test2 test3 /;
Hope that helps,
Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: call sub_function easily
by kevbot (Vicar) on Mar 03, 2019 at 23:47 UTC |