Perl array pop() function - Quick Tutorial

How to use the array pop() function

Man working at a computer
Radius Images/Getty Images
 $ITEM = pop(@ARRAY); 
Perl's pop() function is used to remove and return (or pop) the last element from an array, which reduces the number of elements by one. The last element in the array is the one with the highest index. It's easy to confuse this function with shift(), which removes the first element from an array.
 @myNames = ('Larry', 'Curly', 'Moe');
 $oneName = pop(@myNames); 
If you think of an array as a row of numbered boxes, going from left to right, it would be the element on the far right.
The pop() function would cut the element off the right side of the array, return it, and reduce the elements by one. In the examples, the value of $oneName becomes 'Moe', the last element, and @myNames is shortened to ('Larry', 'Curly').

The array can also be thought of as a stack - picture of a stack of numbered boxes, starting with 0 on the top and increasing as it goes down. The pop() function would pop the element off the bottom of the stack, return it, and reduce the elements by one.

 @myNames = (
 'Larry',
 'Curly',
 'Moe'
 );
 $oneName = pop(@myNames);