Broad Network


Function Reference and Callback in Perl

Perl Function Explained Part 3

Foreword: In this part of the series I talk about reference to a function; I also illustrate how to code a callback function.

By: Chrysanthus Date Published: 8 Sep 2015

Introduction

This is part 3 of my series, Perl Function Explained. In this part of the series I talk about reference to a function; I also illustrate how to code a callback function. You should have read the previous parts of the series before reaching here, as this is a continuation.

Reference to a Function
Consider the following function:

    sub cb
            {
                my $cb1 = $_[0] +1;
                return $cb1;
            }

This function adds 1 to its only argument.
Now, cb is the name of the function. Assume that there is one argument to this function and it is 5. You can call this function with

    cb(5);

or

    &cb(5);

The use of & when calling a named function is optional.

Consider the following anonymous (no name) function:

    my $coderef = sub
                    {
                        my $cb1 = $_[0] +1;
                        return $cb1;
                    };

You can call this function with,

    &$coderef(5);

Anonymous function definition ends with a semicolon. Now, $coderef is the identifier of the function. Its value is said to be the code reference of the function. The function body is the same as above, adding 1 to its only argument, and then returning the result. You can choose any variable name you want, for $coderef.

If you want a reference to a function, you have to precede the function name with \& . So to return a reference of the above function whose name is cb, you would type:

    \&cb

The code reference ($coderef) is already a reference (has the reference).

Each of these references is to the function definition and not to the function call.

Callback Function
In a callback function scheme, there are two functions: the main function and a smaller function, which is the callback function. In the body of the main function, certain values become arguments to the callback function call. The callback function is actually executed when the main function is called. The body of the callback function is coded in the call of the main function. An argument for the callback function may be a scalar e.g. a string, or it may be an array or a hash. Read and try the following code:

use strict;

    sub mainfn
        {
            my $str= 'We are learning.';
            my @colorArr = ('blue', 'green', 'red');

            $_[1]($str, @colorArr);
        }

    mainfn('dummy', sub
        {
            print $_[0], "\n";
            print $_[1], $_[2], $_[3];
        });

In the main function, the callback function is identified by $_[1]. The callback function is called in the main function with arguments: $str and @colorArr. However, the callback function is defined in the call of the main function. So, when the main function is called, its definition calls the callback function. The definition of the callback function is an argument of the call of the main function. The definition of the callback function here, takes no function name.

The following code is the same as the above but code reference is used in the callback function definition.

use strict;

    sub mainfn
        {
            my $str= 'We are learning.';
            my @colorArr = ('blue', 'green', 'red');

            $_[1]($str, @colorArr);
        }

    mainfn('dummy', my $coderef = sub
        {
            print $_[0], "\n";
            print $_[1], $_[2], $_[3];
        });

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