我发现在 perl 中调度许多函数的最快方法是使用函数引用。剩下的问题是,我必须在调度程序和函数模块中的 ($func1, $func2, ...) 列表中包含函数名称。我找不到任何方法来包含它们,就像 C include 会做的那样。这是我的代码:主模块:
use strict;
our ($base);
$base = '/home/en/dtest/perl/forditas/utf8/forditas/test1';
require("$base/disph1.pl");
require("$base/fut1h1.pl");
for (my $j = 0; $j < 5; $j++){
dispatch($j);
}
调度模块:
use strict;
our ($base);
require("$base/fut1h1.pl");
our ($sref1, $sref2, $sref3, $sref4, $sref5); # This is what I'd like to include
my %shash = (
'0' => $sref1,
'1' => $sref2,
'2' => $sref3,
'3' => $sref4,
'4' => $sref5,
);
sub dispatch($){
my ($ix) = @_;
my ($a, $b, $c);
$a = 1; $b = 2; $c = 3;
my $ref = $shash{$ix};
&$ref($a,$b, $c);
}
1;
功能模块:
use strict;
our ($sref1, $sref2, $sref3, $sref4, $sref5); # This is what I'd like to include
$sref1 = sub($$$) {
my ($a,$b,$c) = @_;
print "sub1 $a,$b,$c\n";
};
$sref2 = sub($$$) { my ($a,$b,$c) = @_; print "sub2 $a, $b, $c\n"; };
$sref3 = sub { print "sub3\n"; };
$sref4 = sub { print "sub4\n"; };
$sref5 = sub { print "sub5\n"; };
1;
这是运行的结果:
$ perl enhufh1.pl
sub1 1,2,3
sub2 1, 2, 3
sub3
sub4
sub5
提前感谢您的提示。