pop 
pop ARRAY
popThis function treats an array like a stack—it pops (removes)
and returns the last value of the array, shortening the array by one
element. If ARRAY is omitted, the function
pops @_ within the lexical scope of
subroutines and formats; it pops @ARGV at file scopes (typically the main
program) or within the lexical scopes established by the eval STRING,
BEGIN {}, CHECK {}, UNITCHECKINIT {}, and END {} constructs. It has the same effect
as:
$tmp = $ARRAY[$#ARRAY––];
or:
$tmp = splice @ARRAY, –1;
If there are no elements in the array, pop returns undef. (But don’t depend on that to tell you
when the array is empty if your array contains undef values!) See also push and shift. If you want to pop more than one
element, use splice.
The pop requires its first
argument to be an array, not a list. If you just want the last element
of a list, use this:
( LIST )[–1]
Starting with v5.14, pop can
take a reference to an unblessed array, which will be dereferenced
automatically. This aspect of pop is
considered experimental. The exact behavior may change in a future
version of Perl.
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