Broad Network


Perl Arrow and Binding Operators

Perl Operators – Part 7

Perl Course

Foreword: In this part of the series, I talk about the arrow and the binding operators in Perl.

By: Chrysanthus Date Published: 12 Oct 2015

Introduction

This is part 7 of my series, Perl Operators. In this part of the series, I talk about the arrow and the binding operators in Perl. The arrow operator is -> . The binding operator is =~. You should have read the previous parts of the series before reaching here, as this is a continuation.

Arrow Operator
The arrow operator is -> . It is a dereference operator, but you need to know how to use it. It is used with arrays, hashes, function calls, and in OOP.

Arrow Operator with the Array
Consider the following array statement:

    my $aRef = ['dog', 'cat', 'cow', 'sheep'];

Here, the variable is a reference to an array. When an array is enclosed by […], the […] returns a reference to the array. The third value, “cow” can be obtained from $aRef either using,

    ${$aRef}[2]

which can be abbreviated to

    $$aRef[2]    #taking off {}

or

    $aRef->[2]

In this last case, the array reference has been used directly without {}, but with -> .

Arrow Operator with the Hash
Consider the following hash statement:

    my %hRef = {Apple => "purple", Banana => "yellow", Pear => "green", Lemon => "green"};

Here, the variable is a reference to a hash. When a hash is enclosed by {…}, the {…} returns a reference to the hash. The third value, “green” can be obtained from $hRef either using,

    ${$hRef}{'Pear'}

which can be abbreviated to

    $$hRef{'Pear'}   #taking off {}

or

    $hRef->{'Pear'}

In this last case, the hash reference has been used directly without {}, but with -> .

Arrow Operator with Function Call
Consider the following code reference function definition:

    my $cRef = sub
            {
                print $_[0];
            };

Here, the variable is a reference to a function definition. When a function definition is anonymous (no name), it returns a reference to the definition. Such a function can be called by $cRef using,

    &{$cRef}('seen')

where 'seen' is an argument. The call can be abbreviated to

    &$cRef('seen')   #taking off {}

You can also use,

    $cRef->('seen')

In this last case, the code reference has been used directly without {}, but with -> .

Arrow Operator with OOP
The arrow operator can be used by a class name to call a function or it can be used by the instantiated object to access an attribute or still call a function. The following code illustrates this:

use strict;

    {package TheClass;

        sub fn
            {
                print $_[1], "\n";
            }

        sub new
            {
                bless {}, $_[0];
            }
    }

    my $obj = TheClass->new();
    $obj->fn('Hi there.');
    $obj->{'attrib'} = "one";
    my $var = $obj->{'attrib'};
    print $var;

The output is

    Hi there.
    one

The class name has been used with the arrow operator to call the new() function. The object has been used with the arrow operator to call the method, fn().The object has been used with the arrow operator to access the attribute, attrib.

The Binding Operator
The binding operator is, =~ . Consider the following program:

use strict;

if ("Hello World" =~ /World/)
  {
    print "Matched";
  }
else
  {
    print "Not Matched";
  }

The argument on the left of the operator is called the subject string. The argument on the right is known as the regular expression. The binding operator and arguments are in an if-condition. The operator normally returns true or false. In this case, if the word, “World” is found in the subject, then it would return true. If the word, “World” is not found in the subject, then it would return false.

You can use different right and left arguments, and true or false would be returned depending on whether the right argument is seen in the left argument. Try the above program.

The operator, !~ does the opposite of the binding operator. It is the negated binding operator. Try the following program:

use strict;

if ("Hello World" !~ /World/)
  {
    print "Matched";
  }
else
  {
    print "Not Matched";
  }

This program should display, “Not Matched”; the previous one should have displayed, “Matched”.

The arrow operator is left associative. The binding operator is left associative.

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