Reputation:
I have been reading this code for a while, and I am not able to 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
Upvotes: 4
Views: 172
Reputation: 66968
In object-oriented Perl, a method's invocant (the thing upon which the method was called, either a class or an instance of a class) is passed into the subroutine as the first parameter.
The parameters to subroutines are found in the special array @_
. shift
removes the first element of an array and returns it. If you don't specify an explicit argument to shift
, it works on @_
by default.
The usual pattern for OO methods is to do stuff like
# foo method
sub foo {
my $self = shift;
# do stuff to $self
$self->othermethod;
}
What's going on here is they are just using a shortcut to avoid creating the variable $self
, and calling the state
method on the invocant as returned directly from shift
. So your method is equivalent to:
sub shared {
my $self = shift;
$self->state( broadcast => @_ );
}
Upvotes: 13