etlamar has asked for the wisdom of the Perl Monks concerning the following question:

Hi, PerlMonks. I am seeking wisdom about the shift()-> idiom. What does this do? I understand how shift operates in the context of an array, but this form seems to either be dereferencing a hash/array ref or calling a method. Specifically, I found this form of shift in the source code to Geo::Shapefile, e.g.


sub read_shx_header {shift()->read_shx_shp_header('shx',@_);}

Just curious if someone could shed some light on the use of shift in this context

Thanks

Replies are listed 'Best First'.
Re: Perl Shift()->
by tobyink (Canon) on Dec 14, 2014 at 21:20 UTC

    If you don't specify an array for shift and pop to operate on, then they operate on @_ (or if used outside a sub definition, they operate on @ARGV).

    # Which means that this... sub read_shx_header {shift()->read_shx_shp_header('shx',@_);} # Is a shortcut for writing out... sub read_shx_header { my $self = shift @_; $self->read_shx_shp_header('shx', @_); }

      Ah!. Thanks for the insight.

      Thanks much tobyink and GrandFather. The information is very helpful! Best regards.

Re: Perl Shift()->
by GrandFather (Saint) on Dec 14, 2014 at 21:18 UTC

    read_shx_header is a member function. When called the first parameter passed to a member function is self. So in that code shift retrieves self then calls the member function read_shx_shp_header passing 'shx' and the remaining parameters from the original call to read_shx_header. In other words, the code is inserting a parameter ('shx') into the front of the parameter list and chaining to another member function to get the actual work done.

    Perl is the programming world's equivalent of English