Stack in Perl is a linear data structure that follows the LIFO (Last In First Out) or FILO (First In Last Out) order.
In simpler terms, a stack is an array in which insertion and deletion takes place at only one end called the top of the stack.
Pushing is the process of insertion of elements into a stack.
Popping is the process of removal of topmost element of a stack.
Creating a stack in Perl is rather simple. All we need to do is declare an array.
The stack could be empty, as follows:
@stack;
Or it could be initialized:
@stack = (1, 2, 3);
Pushing can be done using either the push() function or the splice() function.
-
Pushing using push():
Syntax: push(@stack, list);
Parameters:- @stack – The stack on which push is to be performed.
- list – The elements to be pushed into a stack. These elements might be scalar, array, hash or any combination of these.
Example:
#!/usr/bin/perl# Intitialising the Stack@stack= (1..3);# Original stackprint"Original Stack: @stack";# Scalar to be pushed$scalar="scalar";# Array to be pushed@array= ("a","r","r","a","y");# Hash to be pushed%hash= ("Geeks"=> 10,"for Geeks"=> 20);# scalars, arrays and hashes can be# inserted at the same timepush(@stack, ($scalar,@array,%hash));# Updated Stack after# Push operationsprint("\nUpdated Stack: @stack");chevron_rightfilter_noneOutput:Original Stack: 1 2 3 Updated Stack: 1 2 3 scalar a r r a y Geeks 10 for Geeks 20
-
Pushing using splice():
Syntax: splice(@stack, scalar(@stack), 0, list);
Parameters:- splice() function appends the ‘list’ at the end of @stack.
- THe ‘list’ could be a scalar, an array or a hash.
Example:
#!/usr/bin/perl# Intitialising the Stack@stack= (1..3);# Original stackprint"Original Stack: @stack";# Scalar to be pushed$scalar="scalar";# Array to be pushed@array= ("a","r","r","a","y");# Hash to be pushed%hash= ("Geeks"=> 10,"for Geeks"=> 20);# scalars, arrays and hashes can be# inserted at the same timesplice(@stack,scalar(@stack), 0,($scalar,@array,%hash));# Updated Stack after# Push operationsprint("\nUpdated Stack: @stack");chevron_rightfilter_noneOutput:Original Stack: 1 2 3 Updated Stack: 1 2 3 scalar a r r a y Geeks 10 for Geeks 20
Popping can be done using either the pop() function or the splice() function.
-
Popping using pop():
Syntax: $popped_element = pop(@stack);
Parameters:- pop() function returns the popped element.
- $popped_element contains the element popped from the stack.
Example:
#!/usr/bin/perl# Intitialising the Stack@stack= (1..3);# Original stackprint"Original Stack: @stack";# Topmost element i.e. 3 is# removed and returned$popped_element=pop(@stack);# Printing popped elementprint"\nPopped element: $popped_element";# Updated Stack after# Pop operationprint("\nUpdated Stack: @stack");chevron_rightfilter_noneOutput:Original Stack: 1 2 3 Popped element: 3 Updated Stack: 1 2
- If the stack is empty, undef is returned. undef is analogous to NULL in Java and None in Python. However, no error is raised.
Example:
#!/usr/bin/perl# Creating a Stack@stack;# undef is returned since the# stack is empty.# No error is raised.$popped_element=pop(@stack);# Printing popped element# Since it contains no value,# hence a blank space is returnedprint"Popped element: $popped_element";chevron_rightfilter_noneOutput:Popped element:
-
Popping using splice()::
Syntax: $popped_element=splice(@stack, -1);
Parameters:- splice() function removes the last element of the stack and returns it.
- $popped_element stores the returned value.
Example:
#!/usr/bin/perl# Intitialising the Stack@stack= (1..3);# Original stackprint"Original Stack: @stack";# popping using splice()$popped_element=splice(@stack, -1);# Printing popped elementprint"\nPopped element: $popped_element";# Updated Stack after# Pop operationprint("\nUpdated Stack: @stack");chevron_rightfilter_noneOutput:Original Stack: 1 2 3 Popped element: 3 Updated Stack: 1 2
- An error is raised, if the stack is empty. The following code raises an error:
- Perl | Implementing a Queue
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Implementing callback in PHP
- Implementing next_permutation() in Java with Examples
- Perl | split() Function
- Perl | chomp() Function
- Perl | Backtracking in Regular Expression
- Perl | Searching in a File using regex
- Perl | Operators | Set - 1
- Perl | lt operator
- Perl | chop() Function
- Perl | rename() Function
- Perl | Subroutines or Functions
- Perl | Decision Making (if, if-else, Nestedâif, if-elsif ladder, unless, unless-else, unless-elsif)
- Perl | Scalars
- Perl | Loops (for, foreach, while, do...while, until, Nested loops)
- Number Guessing Game using Perl
- Perl | Comparing Scalars
- Perl | Removing leading and trailing white spaces (trim)
usewarnings;#!/usr/bin/perlusewarnings;# Creating a Stack@stack;# popping using splice()# An error is raised here$popped_element=splice(@stack, -1);# Printing popped elementprint"\nPopped element: $popped_element";# Updated Stack after# Pop operationprint("\nStack: @stack");chevron_rightfilter_noneRuntime Error:
Useless use of a variable in void context at /home/59c7c19979aa9e46564cd145d5fe5601.pl line 6.
Modification of non-creatable array value attempted, subscript -1 at /home/59c7c19979aa9e46564cd145d5fe5601.pl line 10.Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

