perl – 带有移位运算符的()裸字有什么用?
发布时间:2020-12-15 21:55:24 所属栏目:大数据 来源:网络整理
导读:我正在学习中间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";
我正在学习中间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"; } } }
你能清楚地解释一下使用带加号运算符的加号的工作吗? 解决方法
没有加号,@ {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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |