What does shift-> somesub () do in Perl?

I have been reading this code for a while, and I cannot figure it out. What does the following do?

sub shared { shift->state(broadcast => @_) } 

https://metacpan.org/source/GRAY/WebService-Google-Reader-0.21/lib/WebService/Google/Reader.pm#L72

+4
source share
1 answer

In object-oriented Perl, the invocant method (the object on which the method was called, either the class or an instance of the class) is passed to the subroutine as the first parameter.

Parameters for routines are found in the special @_ array. shift removes the first element of the array and returns it. If you did not specify an explicit shift argument, it works by @_ by default.

The usual pattern for OO methods is to do things like

 # foo method sub foo { my $self = shift; # do stuff to $self $self->othermethod; } 

What happens here is simply to use a shortcut to avoid creating the $self variable and calling the state method on invocant as returned directly from shift . So your method is equivalent:

 sub shared { my $self = shift; $self->state( broadcast => @_ ); } 
+13
source

Source: https://habr.com/ru/post/1386163/


All Articles