Programming Perl

Programming PerlSearch this book
Previous: 3.2.107 pipeChapter 3
Next: 3.2.109 pos

3.2.108 pop


This function treats an array like a stack - it pops and returns the last value of the array, shortening the array by 1. If ARRAY is omitted, the function pops @ARGV (in the main program), or @_ (in subroutines). It has the same effect as:

$tmp = $ARRAY[$#ARRAY--];


$tmp = splice @ARRAY, -1;

If there are no elements in the array, pop returns the undefined value. See also push and shift. If you want to pop more than one element, use splice.

Note that pop requires its first argument to be an array, not a list. If you just want the last element of a list, use this:


Previous: 3.2.107 pipeProgramming PerlNext: 3.2.109 pos
3.2.107 pipeBook Index3.2.109 pos