简体   繁体   English

带有移位运算符的(+)bareword有什么用?

[英]What is the use of (+) bareword with shift operator?

I'm learning the intermediate perl.In that now I'm studying about the object references for class.In that they gave one package 我正在学习中间perl。现在我正在研究class的对象引用。在那里他们给了一个包

{
    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”}

can you anybody clearly explain its work for using plus sign with shift operator? 你能清楚地解释一下使用带加号算子的加号的工作吗?

Without the plus sign, @{shift} is the same as the array @shift which doesn't call the shift operator at all. 没有加号, @{shift}与数组@shift相同,后者根本不调用shift运算符。 Adding the plus sign forces shift to be evaluated as an expression , so the shift operator is called 添加加号强制shift将被计算为表达式 ,因此调用shift运算符

I would prefer to see @{ shift() } 我更愿意看到@{ shift() }

Methods are normally written so that they extract the first parameter to $self , like this 通常编写方法,以便将第一个参数提取到$self ,就像这样

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM