微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl – 带有移位运算符的()裸字有什么用?

我正在学习中间perl.现在我正在研究类的对象引用.它们给了一个
{
    package Barn;

    sub new { bless [],shift }

    sub add { push @{ +shift },shift }

    sub contents { @{ +shift } }

    sub DESTROY {
        my $self = shift;
        print "$self is being destroyed...\n";
        for ( $self->contents ) {
            print ' ',$_->name," goes homeless.\n";
        }
    }
}

in this I can’t understand the work of plus sign with shift
operator. In text they said,the plus sign is like bareword it would
be interpreted as a soft reference: @{“shift”}

你能清楚地解释一下使用带加号运算符的加号的工作吗?

解决方法

没有加号,@ {shift}与数组@shift相同,后者根本不调用移位运算符.添加加号强制转移将被计算为表达式,因此调用移位运算符

我更愿意看到@ {shift()}

通常编写方法,以便将第一个参数提取到$self,就像这样

sub new {
    my $class = shift;
    bless [ ],$class;
}

sub add {
    my $self = shift;
    push @$self,shift;
}

sub contents {
    my $self = shift;
    return @$self;
}

原文地址:https://www.jb51.cc/Perl/171854.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐