Broad Network


Perl Reference to an Anonymous Subroutine

Perl References - Part 5

Forward: In this part of the series, we look at Perl Reference to an anonymous subroutine (function).

By: Chrysanthus Date Published: 12 Aug 2012

Introduction

This is part 5 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 look at Perl Reference to an anonymous subroutine (function). In Perl, a function is called a subroutine, abbreviated, sub. Anonymous means no name.

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.

Named and Anonymous Subroutine
Anonymous means, no name. A named subroutine is defined as follows:

sub subName
    {
        #statements
    }

where subName is the name of the subroutine (function). An anonymous subroutine is defined as follows:

sub
    {
        #statements
    }

As you can see there is no name for this subroutine. A subroutine should be identified. Instead of having a name, you can have a reference that refers (points) to it. You do this as follows:

$coderef = sub
                    {
                        #statements
                    };

Here you have a statement and so note the semicolon just after the closing brace, }. $coderef is a scalar that holds a reference to the subroutine (region) in memory.

Note: sub{} is an operator that returns a reference.

Calling a Subroutine using its Reference
You call a subroutine using its reference as follows:

    &$coderef;

or

    &$coderef(argument list);

You begin with & and then the scalar variable that holds the reference. The first statement above is used when there is no argument; the second statement is used when there are arguments. The first statement can still be “&$coderef();” with empty parentheses.

Illustration
Read and try the following code where no argument is sent:

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 $cref = sub
            {
                print "seen";
            };

&$cref;

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

The output of this program is “seen”. Read and try the following code, where three arguments are sent:

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 $cref = sub
            {
                print $_[0] . "<br />";
                print $_[1] . "<br />";
                print $_[2] . "<br />";
            };

&$cref("aaa", "bbb", "ccc");

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

The output of this program are the three arguments in three lines.

Returning
A function (subroutine) as the ones above can return a value or a reference, depending on what it does. The following code shows how to handle whatever is returned.

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 $cref = sub
            {
             return "seen";
            };

my $ret = &$cref;
print $ret;

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

That is: as you call the function with &, just assign what is returned to a variable.

We end here for this part of the series. We 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

Comments

Become the Writer's Fan
Send the Writer a Message