If used in the main program, it will shift (remove and return) the first value from @ARGV, the argument list of your program. If used inside a subroutine, it will shift the first value from @_, the argument list of the sub.
'shift' accepts an array and extracts the first element (actually removing it from the array). If you don't provide any array, it uses @ARGV (the arguments to the command line of the program) to read the data. If you're inside a function, it uses @_, namely, the parameters of the function.
In all cases, shift removes the element at list index 0 and returns it, shifting all remaining elements down one.
Inside a sub, shift without an argument (bare shift) operates on the @_ list of subroutine parameters.
Suppose I call mysub('me', '22')
sub mysub {
my $self = shift; # return $_[0], moves everything else down, $self = 'me', @_ = ( '22' )
my $arg1 = shift; # returns $_[0], moves everything down, $arg1 = '22', @_ = ()
}
If you call a method on an object like this:
$obj->my_method('one');
Then the my_method subroutine will actually be passed two arguments in @_. The first is the object that the method was invoked on (sometimes called the invocant) which in this example is $obj. Whenever you call the new() method, Perl automatically passes the class name (eg. Animals) as the first argument to the special array @_. The second argument will be the string 'one'.
It is very common in object-oriented code to start a method definition like this:
sub my_method {
my $self = shift;
}
Which means $self will now contain the invocant (the class/object that invoked the function) and @_ will be left with all the remaining arguments.