use strict;
use warnings;
sub test1 {
my $arg = shift;
print "$arg";
}
my $rs = \&test1;
sub test2 {
my $value = shift;
print "$value \n";
return $rs;
}
&test2("hello")->("Bye");
它工作正常。但是如果在test1子目录中,我们想从test2子目录传递参数。就像是
use strict;
use warnings;
sub test1 {
my $arg = shift;
print "$arg";
}
my $rs = \&test1;
sub test2 {
my $value = shift;
print "$value \n";
return $rs($value);
}
&test2("hello")->();
我知道语法错误,但是不知道该怎么做。我希望问题清楚。
我希望输出为hello hello
不知道该怎么做
像一样调用Coderef $coderef->(@args)
。例如
sub hello {
my $name = shift;
print "Hello $name\n";
}
sub invoke {
my ($code, @args) = @_;
$code->(@args);
}
invoke(\&hello, "World");
输出:Hello World
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句