Broad Network


Perl Function Passing and Returning of a Reference

Perl References - Part 4

Forward: In this part of the series, we see how to pass a reference to a function in Perl and how to return a reference.

By: Chrysanthus Date Published: 12 Aug 2012

Introduction

This is part 4 of my series, Perl References. You should have read the previous parts of the series before reading this one. The titles of the different parts of the series are given at the bottom of this article. In this part of the series, we see how to pass a reference to a function in Perl and how to return a reference. Before that we shall 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.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

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;

print "Content-Type: text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>Code Sample</title>\n";
print "</head>\n";
print "<body>\n";

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);

print "</body>\n";
print "</html>\n";

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.

Pass 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;

print "Content-Type: text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>Code Sample</title>\n";
print "</head>\n";
print "<body>\n";

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

sub mySub
    {
        print ${$_[0]} . "<br /><br />";

        print ${$_[1]}[0] . "<br />";
        print ${$_[1]}[1] . "<br /><br />";

        print $_[2]->{'four'} . "<br />";
        print $_[2]->{'five'} . "<br />";
    }

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

print "</body>\n";
print "</html>\n";

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 Reference
Object Oriented Programming in Perl
Date and Time in Perl
Regular Expressions in Perl
Perl Course
Web Development Course
Major in Website Design
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message