Geo*_*Geo 7 perl reference coderef
这是代码:
sub function($&) {
my $param1 = shift;
my $code = shift;
# do something with $param1 and $code
}
Run Code Online (Sandbox Code Playgroud)
如果我试着像这样打电话:
function("whatever") {
print "i'm inside the coderef\n";
}
Run Code Online (Sandbox Code Playgroud)
我得到Not enough arguments for MyPackage::function at x.pl line 5, near ""whatever" { ".如何在不必添加sub代码块前调用它?
Sea*_*ean 18
首先放入coderef参数:
sub function (&$) {
my $code = shift;
my $param1 = shift;
# do something with $param1 and $code
}
function { print "i'm inside the coderef\n" } "whatever";
Run Code Online (Sandbox Code Playgroud)
请参阅perlsub手册页,其中部分内容如下:
"&"需要一个匿名子例程,如果作为第一个参数传递,则不需要"sub"关键字或后续逗号.
| 归档时间: |
|
| 查看次数: |
212 次 |
| 最近记录: |