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

调用Perl子时使用&符号和括号

发布时间:2020-12-15 21:15:07 所属栏目:大数据 来源:网络整理
导读:#!/usr/bin/perlsub t { print "in tn"; print "@_n"; s;}sub s { print "in sn"; print "@_n";}t(1,2);print "outn";print "@_n"; 输出: in t1 2in s1 2out 如您所见,当没有参数通过时,输出1 2.这是一个功能还是一个bug? 测试版本为5.8.8. 解决方法
#!/usr/bin/perl

sub t {
  print "in tn";
  print "@_n";
  &s;
}

sub s {
  print "in sn";
  print "@_n";
}

t(1,2);
print "outn";
print "@_n";

输出:

in t
1 2
in s
1 2
out

如您所见,当没有参数通过时,输出1 2.这是一个功能还是一个bug?

测试版本为5.8.8.

解决方法

使用& NAME调用子程序;语法使当前@_可见.这在 perlsub记录:

If a subroutine is called using the & form,the argument list is optional,and if omitted,no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid.

所以这绝对是一个功能.

(编辑:李大同)

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

    推荐文章
      热点阅读