shift 
shift ARRAY
shiftThis function shifts the first value of the array off and
returns it, shortening the array by one and moving everything down. (Or
up, or left, depending on how you visualize the array list. We like
left.) If there are no elements in the array, the function returns
undef.
If ARRAY is omitted, the function
shifts @_ within the lexical scope of
subroutines and formats; it shifts @ARGV at file scopes (typically the main
program) or within the lexical scopes established by the eval STRING,
BEGIN {}, CHECK {}, UNITCHECK
{}, INIT {}, and END {} constructs.
Subroutines often start by copying their arguments into lexical
variables, and shift can be used for
this:
sub marine {
my $fathoms = shift; # depth
my $fishies = shift; # number of fish
my $o2 = shift; # oxygen concentration
# ...
}shift is also used to process
arguments at the front of your program:
while (defined($_ = shift)) {
/^[^–]/ && do { unshift @ARGV, $_; last };
/^–w/ && do { $WARN = 1; next };
/^–r/ && do { $RECURSE = 1; next };
die "Unknown argument $_";
}You should consider the standard Getopt::Std and Getopt::Long modules for processing program arguments.
Starting with v5.14, shift can
take a reference to an unblessed array, which will be dereferenced
automatically. This aspect of shift
is considered experimental. The exact behavior may change in a future
version of Perl.
See also unshift, push, pop, and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access