Broad Network


Reference to an Anonymous Subroutine in Perl

Perl References Optimized – Part 5

Perl Course

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

By: Chrysanthus Date Published: 10 Jul 2015

Introduction

This is part 5 of the series, Perl References Optimized. 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. You should have read the previous parts of the series before reaching here, as this is a continuation.

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;

my $cref = sub
            {
                print "seen";
            };

&$cref;

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

use strict;

my $cref = sub
            {
                print $_[0] . "\n";
                print $_[1] . "\n";
                print $_[2] . "\n";
            };

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

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;

my $cref = sub
            {
             return "seen";
            };

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

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.

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

Comments

Become the Writer's Fan
Send the Writer a Message