Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple pop in one line

I found a line of Perl code which uses pop in a way that I've never seen before. I always thought that pop returns exactly one item from an array, but the following usage does differently:

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. How exactly does this work?

like image 735
Nate Glenn Avatar asked Feb 21 '26 02:02

Nate Glenn


1 Answers

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; saving first 3 elements into $self, $loop, and $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"

like image 200
PSIAlt Avatar answered Feb 23 '26 06:02

PSIAlt