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;
}

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...