eWebProgrammer eweb  





Perl Variables  «Prev 
Using an array as a stack

Stack pop

Diagram of pop from the stack
Diagram of pop from the stack
Notice that the pop function returns the item removed as its value.
This allows you to assign it to a variable or use it in an expression:
$pic = pop @bestpix;

Abstraction

Think about a stack in an abstract way. Since it does not hold any particular kind of element (like books) and we aren't restricting ourselves to any particular programming language or any particular implementation of a stack.
Stacks hold objects, usually all of the same type. Most stacks support just the simple set of operations we introduced above; and thus, the main property of a stack is that objects go on and come off of the top of the stack.
Here are the minimal operations we'd need for an abstract stack (and their typical names):
  1. Push: Places an object on the top of the stack.
  2. Pop: Removes an object from the top of the stack and produces that object.
  3. IsEmpty: Reports whether the stack is empty or not.
Because we think of stacks in terms of the physical analogy, we usually draw them vertically (so the top is really on top).