Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does shift->{o} = $o; do in Perl?

Tags:

perl

What this line in subroutine mean?

shift->{o} = $o;

I know what shift usualy do, but don't understand it in this context, with dash and arrow.

like image 595
Barvajz Avatar asked Sep 21 '14 16:09

Barvajz


People also ask

What does the shift function do in Perl?

The shift() method in Perl is used to return the first value of an array. It removes the first element and shifts the array list elements to the left by one.

What is shift @_?

shift uses the @_ variable which contains the values passed to a function or shift uses @ARGV for the values passed to the script itself if shift is referenced outside of a function.

What does @_ mean in Perl?

Using the Parameter Array (@_) Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order.


2 Answers

Inside a sub/method,

shift

is short for

shift(@_)

A sub call places the arguments in @_. A method call does the same, but precedes the arguments with the invocant.

If this is in a sub called as a sub, it assigns $o to the element o of the hash referenced by the first argument.

If this is in a sub called as a method, it assigns $o to the element o of the hash referenced by the invocant. Effectively, this sets an attribute o of the object on which this method was called.

In the process, shift removes the reference from @_, though I suspect that might be of no consequence.

like image 63
ikegami Avatar answered Sep 28 '22 04:09

ikegami


Interprets the value shifted as a hashref and assigns a value for the 'o' key in that hash.

like image 45
ysth Avatar answered Sep 28 '22 04:09

ysth