Skip to content
Related Articles

Related Articles

Perl | Passing Complex Parameters to a Subroutine
  • Last Updated : 07 May, 2019

Prerequisite: Perl | Subroutines or Functions

A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language, the user wants to reuse the code. So the user puts the section of code in a function or subroutine so that there will be no need to rewrite the same code again and again. For this reason, function or subroutine is used in every programming language. These functions or subroutines can take various different types of data structures as parameters. Some of these are explained below:

  1. Passing Lists or Arrays to a Subroutine
  2. Passing References to a Subroutine
  3. Passing Hashes to a Subroutine
  4. Passing File Handles to a Subroutine

Passing Lists or Arrays to a Subroutine: An array or list can be passed to the subroutine as a parameter and an array variable @_ is used to accept the list value inside of the subroutine or function.

Example 1: Here a single list is passed to the subroutine and their elements are displayed.




#!/usr/bin/perl 
  
# Defining Subroutine
sub Display_List 
      
    # array variable to store 
    # the passed arguments 
    my @List1 = @_
          
    # Printing the passed list elements 
    print "Given list is @List1\n"
  
# Driver Code
  
# passing list 
@list = (1, 2, 3, 4); 
  
# Calling Subroutine with 
# list parameter 
Display_List(@list); 
Output:



Given list is 1 2 3 4

Example 2: Here two lists are passed to the subroutine and their elements are displayed.




#!/usr/bin/perl 
  
# Defining Subroutine
sub Display_List 
      
    # array variable to store 
    # the passed arguments 
    my @List3 = @_
          
    # Printing the passed lists' elements
    print "Given lists' elements are @List3\n"
  
# Driver Code
  
# passing lists
@List1 = (1, 2, 3, 4); 
@List2 = (10, 20, 30, 40); 
  
# Calling Subroutine with 
# list parameters
Display_List(@List1, @List2); 
Output:
Given lists' elements are 1 2 3 4 10 20 30 40

Example 3: Here a scalar argument and list is passed to the subroutine and their elements are displayed.




#!/usr/bin/perl 
  
# Defining Subroutine
sub Display_List 
      
    # array variable to store 
    # the passed arguments 
    my @List2 = @_
          
    # Printing the passed list and scalar elements
    print "List and scalar elements are @List2\n"
  
# Driver Code
  
# passing lists
@List = (1, 2, 3, 4); 
  
# passing scalar argument 
$scalar = 100;
  
# Calling Subroutine with 
# list parameters
Display_List(@List, $scalar); 
Output:
List and scalar elements are 1 2 3 4 100

Passing References to a Subroutine: References can also be passed to the subroutines as a parameter. Here the reference of the given array is passed to the subroutine and the maximum value of array elements is returned.

Example:




#!/usr/bin/perl
use warnings;
use strict;
  
# Creating an array of some elements
my @Array = (10, 20, 30, 40, 50);
  
# Making reference of the above array
# and calling the subroutine with 
# reference of the array as the parameter
my $m = max(\@Array);
  
# Defining subroutine
sub max
{
      
    # Getting the array elements
    my $Array_ref = $_[0];
    my $k = $Array_ref->[0];
  
    # Iterating over each element of the 
    # array and finding maximum value
    for(@$Array_ref)
    {
        $k = $_ if($k < $_);
    }
    print "The max of @Array is $k\n";
}
Output:
The max of 10 20 30 40 50 is 50

Passing Hash to a Subroutine: A Hash can also be passed to a subroutine as a parameter and its key-value pair is displayed.

Example:




#!/usr/bin/perl 
  
# Subroutine definition 
sub Display_Hash 
      
    # Hash variable to store 
    # the passed arguments 
    my (%Hash_var) = @_
          
    # Displaying the passed list elements 
    foreach my $key (keys %Hash_var
    
        my $value = $Hash_var{$key}; 
        print "$key : $value\n"
    
  
# Driver Code
  
# defining hash 
%Hash = ('Company' => 'GeeksforGeeks'
         'Location' => 'Noida'); 
  
# calling Subroutine with hash parameter 
Display_Hash(%Hash); 
Output:
Company : GeeksforGeeks
Location : Noida

Passing File Handles to a Subroutine: For creating a file or accessing the file contents one needs a filehandle which is nothing but a structure which is used along with the operators to access the file in a certain mode like reading, writing, appending, etc. FileHandles can also be passed to a subroutine as a parameter to perform various operations on Files.
In the below example, a filehandle is passed to a subroutine:-
Example:




#!/usr/bin/perl 
     
sub printem
{
    my $file = shift;
   
    while (<$file>) {
         print;
    }
}
   
open(fh, "Hello.txt") or die "File '$filename' can't be opened";
   
printem *fh;
Output:
My Personal Notes arrow_drop_up
Recommended Articles
Page :