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.
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.
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.
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.
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.
Interprets the value shifted as a hashref and assigns a value for the 'o' key in that hash.
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