Broad Network


Perl Function in a Hash

Perl Function Explained Part 5

Perl Course

Foreword: In this part of the series I explain how to make a Perl function an element of a hash.

By: Chrysanthus Date Published: 8 Sep 2015

Introduction

This is part 4 of my series, Perl Function Explained. In this part of the series I explain how to make a Perl function an element of a hash. To make a function an element of a hash, use the anonymous function scheme. You should have read the previous parts of the series before coming here, as this is a continuation.

The Anonymous Function Scheme
This scheme does not use a name to define a function; the function is anonymous. An example of its coding is:

use strict;

    my $coderef = sub
            {
                #statements
                print "seen";
            };

    &$coderef();

An anonymous function definition ends with a semicolon. However, in a hash, you omit the semicolon.

Hash by Parentheses
The following is a hash created using the hash parentheses and a function is an element of the hash:

use strict;

    my %ha = (
             apple=>"purple",
             coderef=>sub {
                            #statements
                            print "seen";
                        },
             banana=>"yellow"
             );

    &{$ha{coderef}};

In the hash, the elements are separated by commas. So, the function body is separated from the next element by a comma (not a semicolon). In the hash, $ha{coderef} holds the code reference. Note how the function call reference, $ha{coderef} has been dereferenced by placing it inside &{ }.

The function can have arguments as in the following code:

use strict;

    my %ha = (
             apple=>"purple",
             coderef=>sub {
                            #stateents
                            print $_[0];
                        },
             banana=>"yellow"
             );

    &{$ha{coderef}}("seen");

Note that in the function call, the list of argument(s) are placed after the dereferenced code (not inside).

Hash by Assignment
The first code above can be replaced by the following:

use strict;

    my %ha;

    $ha{apple} = "purple";
    $ha{coderef} = sub {
                            #statements
                            print "seen";
                        };
    $ha{banana} = "yellow";

    &{$ha{coderef}};

Inside the hash you use =>. Outside, you use =. Any assignment statement (anonymous function) ends with a semicolon.

The code above where the function has an argument, can be replaced by the following:

use strict;

    my %ha;

    $ha{apple} = "purple";
    $ha{coderef} = sub {
                            #statements
                            print $_[0];
                        };
    $ha{banana} = "yellow";

    &{$ha{coderef}}("seen");

You can have more than one subroutine (function) in a hash.

That is it 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