You're probably after an symbolic reference, and were almost but not quite there with your snippet...
sub function_1 { print "One\n" }
sub function_2 { print "Two\n" }
{ # Allow symbol table manipulation.
# You do use strict;, right? :)
no strict 'refs';
foreach my $no ( 1 .. 2 ) {
&{ "function_$no" }; # <--- THIS THING
}
}
... but that could potentially blow up in your face if $no turned out to be something unexpected unless you wrapped the whole shebang inside an eval or made good use of can.
The more preferable (and use strict;-happy) way of doing this sort of thing is using something called a dispatch table, which'd look something like ...
sub function_1 { print "One\n" }
sub function_2 { print "Two\n" }
my %functions = (
'DEFAULT' => sub { print "Huh?\n" },
1 => \&function_1,
2 => \&function_2,
);
foreach my $no ( 1 .. 3 ) {
$no = 'DEFAULT' unless exists $functions{$no};
$functions{$no}->();
}
--k.