Removes and returns the first element of an array. This shortens the array by one and moves everything down.
my @arr = ('cat', 'dog');
my $item = shift(@arr); # 'cat'
# @arr is now ('dog');
Returns undef
if the array is empty.
Note: shift
may also return undef
if the first element in the array is undef
.
my @arr = (undef, 'two', 'three');
my $item = shift(@arr); # undef
If ARRAY is omitted, shift
operates on the @ARGV
array in the main program, and the @_
array in subroutines. shift
will operate on the @ARGV
array in eval STRING
, BEGIN {}
, INIT {}
, CHECK {}
blocks.
Starting with Perl 5.14, an experimental feature allowed shift
to take a scalar expression. This experiment has been deemed unsuccessful, and was removed as of Perl 5.24.
See also unshift
, push
, and pop
. shift
and unshift
do the same thing to the left end of an array that pop
and push
do to the right end.