Perl | Passing Complex Parameters to a Subroutine

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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); 

chevron_right


Output:

Given list is 1 2 3 4

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

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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); 

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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); 

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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";
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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); 

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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;

chevron_right


Output:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.