Broad Network


Reference Passing to a Function and Return in Perl

Perl References Optimized – Part 4

Perl Course

Foreword: In this part of the series, you will learn how to pass a reference to a function in Perl and how to return a reference.

By: Chrysanthus Date Published: 10 Jul 2015

Introduction

This is part 4 of the series, Perl References Optimized. In this part of the series, you will learn how to pass a reference to a function in Perl and how to return a reference. Before that you will first see the effects of passing scalars, arrays and hashes to Perl functions in order to make the comparisons with passing references. In Perl, a function is called a subroutine, abbreviated, sub. You should have read the previous parts of the series before reaching here, as this is a continuation.

Passing Non-References to a Function
You can pass a combination of scalars and/or arrays and/or hashes to a Perl subroutine. When you do this, the arrays and the hashes become flattened out and they loose their identities. The elements of any array or hash or scalars become elements of the predefined array @_ in the order in which they were sent as arguments for the scalars and array elements; the order for the hash elements in @_ is not necessarily the order defined for the hash. All this occurs immediately the function starts executing. Read and try the following code that illustrates this:

use strict;

my @arr = ("bbb", "ccc", "ddd");
my %ha = (four => "eee", five => "fff", six =>"ggg");

sub mySub
    {
        print $_[0], " ", $_[1], " ", $_[2], " ", $_[3], " ", $_[4], " ", $_[5], " ", $_[6], " ", $_[7], " ", $_[8], " ", $_[9];
    }

mySub("aaa", @arr, %ha);

Remember; the elements of @_ are accessed with $_[0], $_[1], $_[2], etc. As said above, the hash key/value pairs may not be in the @_ array in the order sent; that is how Perl operates for now.

With the flatness in the @_ array the function cannot return the scalar, array or hash as they were sent if it does not have pre-knowledge of the scalar, array and hash sent. Even if it had, it would need extra code to determine where the hash key/value pairs are in the @_ array. This particular problem is solved if the arguments are passed by reference.

Passing Arguments by Reference
You can pass a scalar, an array or a hash by reference to a function. When you do this, there is no flattening of the array or hash because the elements of the @_ array become the references passed and not the array or hash elements. In the following code, one reference to a scalar, one reference to an array and one reference to a hash are passed. So the @_ array will have three values, which are the three references and not the elements of the scalar, array and hash. So in the function, $_[0] will hold the reference to the scalar, $_[1] will hold the reference to the array and $_[2] will hold the reference to the hash. These are scalar references that you use like ordinary scalar references. Read and try the following code:

use strict;

my @arr = ("bbb", "ccc", "ddd");
my %ha = (four => "eee", five => "fff", six =>"ggg");

sub mySub
    {
        print ${$_[0]} . "\n\n";

        print ${$_[1]}[0] . "\n";
        print ${$_[1]}[1] . "\n\n";

        print $_[2]->{'four'} . "\n";
        print $_[2]->{'five'} . "\n";
    }

my $scal = "aaa";
mySub(\$scal, \@arr, \%ha);

Note: When you pass arguments ordinarily, a copy of the scalar or array element or hash element goes into the @_ array and are stored in different memory regions; you end up with two memory regions for each value. With that, if you change a value in @_ the original value is not changed. When you pass by reference, a copy does not go into the @_ array; so you end up with one memory region for each (same original) value. With this, if you use a reference in @_ to change a value, you change the original value.

Function Returning Arguments
When you pass arguments ordinarily, there is flattening into @_. For the hash, you would not know where a particular value is, in the @_ array in order to return it. When you pass arguments by reference, you can use a reference in @_ to return a value.

You can also return just the reference (address) that was sent as argument. Assume that you are interested in the reference in $_[2], then the following statement will return the reference:

    return $_[2];

If you want the value, you dereference $_[2] depending on whether it is for a scalar, an array or a hash, as we saw in the previous parts of the series. After dereferencing, you can return the value.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message