1: #
2: # Implement a simple stack in terms of a list.
3: #
4:
5: =pod
6:
7: =head1 Stack
8:
9: An object oriented implementation of a Stack data structure.
10: Stacks are first in last out data structures.
11:
12: =head1 Member functions:
13:
14: =cut
15:
16: package Stack;
17:
18: =pod
19:
20: =head2 new
21:
22: Creates a new instance of a stack.
23:
24: my $stack = Stack->new();
25:
26: =cut
27:
28: sub new {
29: my $class = shift; # Class name.
30: my $self = []; # Create an empty list to represent the stack.
31: bless($self, $class); # Turn this into an object and..
32: return $self; # Return it.
33: }
34:
35: =pod
36:
37: =head2 push
38:
39: takes an item and pushes it onto the back end of the stack.
40:
41: my $stack = Stack->new();
42: $stack->push(something);
43:
44: =cut
45:
46: sub push {
47: my $self = shift; # Gets the list...
48: my $item = shift; # The item to push.
49: push(@$self,$item);
50: }
51:
52: =pod
53:
54: =head2 pop
55:
56: Returns the item at the top of the stack: does a pop.
57:
58: my object = Stack->new();
59: my $item = object->pop();
60:
61: =cut
62:
63: sub pop {
64: my $self = shift;
65: return pop(@$self);
66: }
67:
68:
69: 1;
70:
71: =pod
72:
73: =head1 Internal implementation details
74:
75: Stacks are implemented as lists. Thus a stack is a thinly disguised
76: list with push and pop wrappers. Since PERL is a dynamically typed
77: language, stacks can contain any data type ... including a
78: heterogenous collection of types.
79:
80: =cut
81:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>