假设我在Perl中有2个函数。我将创建这两个函数的引用数组。&在命令行参数中,我仅传递调用特定函数的数组索引,如果我不给出任何参数,则它将调用所有引用的函数在array中(默认情况)。
那么,有什么可以帮助我做到这一点的吗?
## Array content function pointers
my @list= {$Ref0,$Ref1 }
my $fun0Name = “fun0”;
my $Ref0 =&{$fun0Name}();
my $fun1Name = “fun1”;
my $Ref1 =&{$fun1Name}();
#### Two functions
sub fun0() {
print "hi \n";
}
sub fun1() {
print "hello \n";
}
##### Now in cmd argument if i passed Test.pl -t 0(index of array ,means call to 1st function)
##### if i give test.pl -t (No option ) ....then i call both function.
创建函数指针(在Perl中称为代码引用)非常容易:
sub foo {
say "foo!";
}
sub bar {
say "bar!";
}
my $foo_ref = \&foo;
my $bar_ref = \&bar;
将事物放入数组非常简单:
my @array = ( $foo_ref, $bar_ref );
从命令行读取参数非常简单:
my $arg = shift @ARGV;
在数组中查找内容也很容易:
my $item = $array[$arg];
您在哪部分遇到问题?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句