加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

当我将coderef传递给这个原型Perl子例程时,为什么会出现语法错误

发布时间:2020-12-15 21:53:57 所属栏目:大数据 来源:网络整理
导读:这是代码: sub function($) { my $param1 = shift; my $code = shift; # do something with $param1 and $code} 如果我试着像这样打电话: function("whatever") { print "i'm inside the coderefn";} 我在x.pl第5行得到MyPackage :: function的参数不足,
这是代码:
sub function($&) {
    my $param1 = shift;
    my $code = shift;
    # do something with $param1 and $code
}

如果我试着像这样打电话:

function("whatever") {
    print "i'm inside the coderefn";
}

我在x.pl第5行得到MyPackage :: function的参数不足,接近“”无论“{”.如何在不必在代码块前添加sub的情况下调用它?

解决方法

首先放入coderef参数:
sub function (&$) {
    my $code = shift;
    my $param1 = shift;
    # do something with $param1 and $code
}

function { print "i'm inside the coderefn" } "whatever";

请参阅perlsub手册页,其中部分内容如下:

An “&” requires an anonymous subroutine,which,if passed as the first argument,does not require the “sub” keyword or a subsequent comma.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读