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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With