[英]Multiple pop in one line
I found a line of Perl code which uses pop
in a way that I've never seen before. 我找到了一行Perl代码,它以我以前从未见过的方式使用
pop
。 I always thought that pop
returns exactly one item from an array, but the following usage does differently: 我一直以为
pop
从数组中返回一个项目,但以下用法的方式不同:
my ($self, $loop, $resources) = @{pop @_};
It seems that the programmer is using one line of code, and one pop
command, to grab three items from the argument array, without creating an explicit for
loop. 程序员似乎正在使用一行代码和一个
pop
命令来从参数数组中获取三个项目,而不创建显式的for
循环。 How exactly does this work? 这究竟是如何工作的?
In this example @_ is an array, and the last element is expected to be an arrayref. 在这个例子中@_是一个数组,并且最后一个元素预计为一个数组引用。
So, pop(@_)
gets the last element from @_ and then it is dereferenced into an array; 因此,
pop(@_)
从@_获取最后一个元素,然后将其解引用到数组中; saving first 3 elements into $self, $loop, and $resources. 将前3个元素保存到$ self,$ loop和$ resources中。
This can be rewritten like this: 这可以像这样重写:
my $self = $_[-1]->[0];
my $loop = $_[-1]->[1];
my $resources = $_[-1]->[2];
Or like this: 或者像这样:
my $temp = pop @_;
my ($self, $loop, $resources) = @$temp;
So, actually it is not "Multiple pop in one line" 所以,实际上它不是“一行中的多个流行音乐”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.