J2EEOnline J2EEOnline


Perl Variables  «Prev 

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.

Perl shift function

The shift function takes the element from the bottom of the stack (element 0):

Diagram of shift

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