Perl Programming/Keywords/shift

Previous: setsockopt Keywords Next: shmctl

The shift keyword

edit

shift shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If the array is empty, undef is returned. Without ARRAY, it shifts @_ within the lexical scope of formats and subroutines.

From Perl 5.14 onward, shift can take a scalar EXPRESSION that must hold a reference to an unblessed array. The argument will be dereferenced automatically. This aspect of shift is considered highly experimental, and the exact behaviour may change in a future version of Perl.

Syntax

edit
  shift ARRAY
  shift EXPRESSION
  shift

Example

edit
unshift(@ARGV, '-e') unless $ARGV[0] =~ /^-/;

See also

edit
Previous: setsockopt Keywords Next: shmctl