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

在这个Perl子程序中使用vars的要点是什么?

发布时间:2020-12-15 22:02:01 所属栏目:大数据 来源:网络整理
导读:在 Mastering Perl年的其中一个章节中,brian d foy从 List::Util开始显示这个代码段: sub reduce(@) { my $code = shift; no strict "refs"; return shift unless @_ 1; use vars qw($a $b); my $caller = caller; local(*{$caller . "::a"}) = my $a; loc
在 Mastering Perl年的其中一个章节中,brian d foy从 List::Util开始显示这个代码段:
sub reduce(&@) {
    my $code = shift;
    no strict "refs";
    return shift unless @_ > 1;
    use vars qw($a $b);
    my $caller = caller;
    local(*{$caller . "::a"}) = my $a;
    local(*{$caller . "::b"}) = my $b;
    $a = shift;
    foreach(@_) {
        $b = $_;
        $a = &{$code}();
    }
    $a;
}

我不明白使用vars qw($a $b)行的含义是什么.即使我评论,我得到相同的输出和警告.

解决方法

这是因为List :: Util在内部使用reduce()函数.

在使用vars的时候,使用该函数时会给出以下警告:

Name "List::MyUtil::a" used only once: possible typo at a.pl line 35.
Name "List::MyUtil::b" used only once: possible typo at a.pl line 35.

您可以通过运行以下代码为自己看到这一点:

use strict;
use warnings;

package List::MyUtil;

sub reduce (&@) {
   # INSERT THE TEXT FROM SUBROUTINE HERE - deleted to save space in the answer
}

sub x {
    return reduce(sub {$a+$b},1,2,3);
}

package main;
my $res = List::MyUtil::x();
print "$resn";

然后使用vars禁用再次运行它.

(编辑:李大同)

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

    推荐文章
      热点阅读