eWebProgrammer eweb  






Perl Variables  «Prev 
Using an array as a stack

Shift and unshift functions in Perl

The shift and unshift functions work just like pop and push.
However, from the other end of the array.
The shift function
The shift function takes the element from the bottom of the stack (element 0):
Beginning PerlBeginning Perl


Diagram of shift
The unshift function
The unshift puts an element on the bottom at element 0.
Diagram of unshift

These functions aren't normally thought of in terms of a stack (although they certainly do work the same); rather they are commonly used to pass parameters to subroutines, including the main function of a Perl program. Their use in that capacity will be covered in detail later in the course.