带移位运算符的 (+) 裸字有什么用?
What is the use of (+) bareword with shift operator?
我正在学习中级perl.In,现在我正在研究class.In的对象引用,他们给了一个包
{
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
相同。添加加号强制 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;
}
我正在学习中级perl.In,现在我正在研究class.In的对象引用,他们给了一个包
{
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
相同。添加加号强制 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;
}