解决方法
使用调度表.
#!/usr/bin/perl use strict; use warnings; use 5.010; sub fields { say 'this is fields'; } sub another { say 'this is another subroutine'; } my %functions = ( fields => \&fields,another => \&another,); my $function = shift; if (exists $functions{$function}) { $functions{$function}->(); } else { die "There is no function called $function available\n"; }
一些例子:
$./dispatch_tab fields this is fields $./dispatch_tab another this is another subroutine $./dispatch_tab xxx There is no function called xxx available