V Perl Stack Pop Function [Diagram]


Perl Variables  «Prev 

Perl Stack Pop Function

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).

Processing Command Line Arguments

Command line arguments are available to any script via the @ARGV array. You can access this directly, either by referencing individual elements or by using the shift and pop functions. The important thing to remember about the @ARGV array is that unlike many other languages (most notably C/C++), element zero is not the name of the script. This is contained in the $0 variable. This means that the first argument is at index zero, and therefore you can print out the entire command line argument list by printing the array:
print join(' ',@ARGV),"\n";

You can also use shift to take off individual arguments in sequence:
$filein = shift;
$fileout = shift;

This technique only works when you use shift in the main script block; calling it within a subroutine takes values off the @_ array instead. It can also sometimes be necessary to put arguments back onto the command line before it is processed, perhaps to insert default or required options. Because the command line arguments are simply available as the @ARGV array, you can push or unshift elements onto the command line. For example:
unshift @ARGV,qw/-v --/ if (@ARGV == 0);

If you want to process command line arguments, rather than doing it yourself, the best method is to use either the standard or extended options supported by the Getopt::Std and Getopt::Long modules, respectively.