Node:Arrays as Stacks, Next:, Previous:Functions, Up:Functions



Arrays as Stacks

What more is a stack than an unbounded array of things? This attitude is seen in Perl through the push and pop functions. These functions treat the "right hand side" (i.e., the end) of the array as the top of the stack. Here is an example:

     use strict;
     my @stack;
     push(@stack, 7, 6, "go");   # @stack is now qw/7 6 go/
     my $action = pop @stack;    # $action is "go", @stack is (7, 6)
     my $value = pop(@stack) +
                 pop(@stack);    # value is 6 + 7 = 13, @stack is empty