pop#
Remove and return the last element of an array.
pop shortens the array by one and hands you the element that was
removed. The array is modified in place. With no argument, pop
operates on @_ inside a subroutine and on @ARGV everywhere
else — the same default target as shift.
Synopsis#
pop @arr
pop @$aref
pop # default: @_ inside a sub, @ARGV outside
What you get back#
The element that was removed, as a scalar. If the array was empty,
the return value is undef and the array stays empty — this is
not an error and emits no warning, so code that always calls
pop in a loop needs its own termination check.
my @empty;
my $x = pop @empty; # $x is undef, no warning
Note that pop also returns undef when the last element was
undef. The two cases are indistinguishable from the return value
alone; use @arr in boolean context, or scalar @arr, to tell
them apart before popping.
my @arr = ('one', 'two', undef);
my $x = pop @arr; # $x is undef — but so would an empty pop be
Global state it touches#
pop with no argument reads @_ (inside a subroutine) or @ARGV
(at file scope, in BEGIN/INIT/CHECK/END blocks, and inside
eval STRING). It does not interact with $_, $,, $\, or any
other punctuation variable.
Examples#
Basic use — pull the last element off a plain array:
my @arr = ('cat', 'dog', 'mouse');
my $item = pop @arr; # $item is 'mouse'
# @arr is now ('cat', 'dog')
LIFO stack — push to add, pop to remove, both at the same end:
my @stack;
push @stack, 'a';
push @stack, 'b';
push @stack, 'c';
my $top = pop @stack; # 'c'; @stack is ('a', 'b')
Discard the last element when you do not need its value. pop in
void context is perfectly valid and is the idiomatic way to drop
the tail:
pop @arr; # shorten @arr by one, value thrown away
Default target inside a subroutine — pop with no argument pops
from @_, which is how a sub peels its last argument off the
argument list:
sub last_arg {
return pop; # pops from @_
}
last_arg(1, 2, 3); # returns 3
Pop through an array reference. Dereference with @$aref (or
@{ $aref } for any non-trivial expression) and pop works
exactly as on a named array:
my $aref = [10, 20, 30];
my $x = pop @$aref; # 30; $aref now points to [10, 20]
Edge cases#
Empty array returns
undef, not an error. No warning is issued, even underuse warnings. A loop of the formwhile (defined(my $x = pop @arr)) { ... }terminates on empty or on a genuineundefelement — usewhile (@arr)instead if the array may legitimately containundef.Bareword expression in place of
ARRAY.poprequires an array, not an arbitrary expression.pop @$arefworks because@$arefis array syntax;pop $arefwas briefly allowed as an experiment between 5.14 and 5.22 and has been a compile error since 5.24. Always write the explicit@sigil on the dereference.Tied arrays.
pop @tiedcalls the tied class’sPOPmethod if defined; otherwise the defaultFETCHSIZE+FETCH+DELETE+STORESIZEfallback applies. Side effects of those methods fire in that order.popshortens the array. Afterpop @arr,$#arrdecreases by one andscalar @arrdrops by one. If you need to inspect the tail without removing it, read$arr[-1]or$arr[$#arr]directly — see$#arrin perlvar.No-argument form changes meaning by scope. At file scope,
pop;meanspop @ARGV. Inside a subroutine it meanspop @_. The same source line therefore behaves differently depending on where it appears — a common surprise when code is refactored into or out of a sub. Write the target explicitly (pop @ARGVorpop @_) whenever the intent is not obvious from context.popon an array aliased through@_. Subroutine arguments are aliases to the caller’s values.pop @_removes the alias entry from@_but does not shorten the caller’s array; to shrink the caller’s array the sub must be passed the array by reference andpop @$arefused instead.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
push— the counterpart; append to the same end of the array thatpopremoves fromshift— remove and return the first element; same defaulting rules aspop(@_in a sub,@ARGVotherwise)unshift— prepend to the front; paired withshiftthe waypushis paired withpopsplice— remove or replace an arbitrary slice;splice @arr, -1is the long-hand form ofpop @arrlast— loop-control keyword, unrelated to array end; listed because the names are often confused$#arr— last index of@arr; changes whenpopshortens the array