--- name: pop signature: 'pop ARRAY' since: 5.0 status: documented categories: ["Arrays"] --- ```{index} single: pop; Perl built-in ``` *[Arrays](../perlfunc-by-category)* # 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`](shift). ## Synopsis ```perl 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`](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. ```perl my @empty; my $x = pop @empty; # $x is undef, no warning ``` Note that `pop` also returns [`undef`](undef) when the last element *was* [`undef`](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. ```perl 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 [`$_`](../perlvar), [`$,`](../perlvar), [`$\`](../perlvar), or any other punctuation variable. ## Examples Basic use — pull the last element off a plain array: ```perl my @arr = ('cat', 'dog', 'mouse'); my $item = pop @arr; # $item is 'mouse' # @arr is now ('cat', 'dog') ``` LIFO stack — [`push`](push) to add, `pop` to remove, both at the same end: ```perl 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: ```perl 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: ```perl 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: ```perl my $aref = [10, 20, 30]; my $x = pop @$aref; # 30; $aref now points to [10, 20] ``` ## Edge cases - **Empty array returns [`undef`](undef), not an error.** No warning is issued, even under `use warnings`. A loop of the form `while (defined(my $x = pop @arr)) { ... }` terminates on empty *or* on a genuine [`undef`](undef) element — use `while (@arr)` instead if the array may legitimately contain [`undef`](undef). - **Bareword expression in place of `ARRAY`.** `pop` requires an array, not an arbitrary expression. `pop @$aref` works because `@$aref` is array syntax; `pop $aref` was 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 @tied` calls the tied class's `POP` method if defined; otherwise the default `FETCHSIZE` + `FETCH` + `DELETE` + `STORESIZE` fallback applies. Side effects of those methods fire in that order. - **`pop` shortens the array.** After `pop @arr`, `$#arr` decreases by one and `scalar @arr` drops by one. If you need to inspect the tail without removing it, read `$arr[-1]` or `$arr[$#arr]` directly — see [`$#arr`](../perlvar) in perlvar. - **No-argument form changes meaning by scope.** At file scope, `pop;` means `pop @ARGV`. Inside a subroutine it means `pop @_`. 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 @ARGV` or `pop @_`) whenever the intent is not obvious from context. - **`pop` on 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* and `pop @$aref` used instead. ## Differences from upstream Fully compatible with upstream Perl 5.42. ## See also - [`push`](push) — the counterpart; append to the same end of the array that `pop` removes from - [`shift`](shift) — remove and return the *first* element; same defaulting rules as `pop` (`@_` in a sub, `@ARGV` otherwise) - [`unshift`](unshift) — prepend to the front; paired with [`shift`](shift) the way [`push`](push) is paired with `pop` - [`splice`](splice) — remove or replace an arbitrary slice; `splice @arr, -1` is the long-hand form of `pop @arr` - [`last`](last) — loop-control keyword, unrelated to array end; listed because the names are often confused - [`$#arr`](../perlvar) — last index of `@arr`; changes when `pop` shortens the array