Broad Network


Scalar and List Context with the Binding Operator

Perl Regular Expressions – Part 6

Perl Course

Foreword: In this part of the series, I illustrate the scalar and list context behavior, of the binding operator.

By: Chrysanthus Date Published: 6 Oct 2015

Introduction

This is part 6 of my series, Perl Regular Expressions. In this part of the series, I illustrate the scalar and list context behavior, of the binding operator. You should have read the previous parts of the series before coming here, as this is a continuation.

Recall: The number 0, the string '0', the string "", the empty list (), and undef are all false in a Boolean context. If a value is not false, it is true. So, if a value is not equivalent to 0, or '0', or "", or (), or undef, then it is true. For example, –1 is true, 1 is true, and 2 is true. In fact, all numbers are true except 0. The string, "something" is true. In fact any non-empty string is true, including " ", which has just a space character.

Scalar Context
In scalar context, the binding expression returns 1 for true and 0 for false. Scalar context is when the expression should be either successful or failed; that is, whether the search item was found or not. List context is when the expression should return a list of at least one string (value). The following is a simple scalar context expression:

    "Hello World" =~ /World/

This expression returns 1, for matching. It would return undef if there was no matching. The return value can be assign to a variable as follows:

    my $var = "Hello World" =~ /World/;

If you print $var, you will have 1 (or undef if matching did not occur – undef is not printable). You can place the expression in a condition as follows:

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

In this case the if-condition boils down to 1 (or undef). In Perl, 1 is true and undef is false.

List Context
List context is when the binding expression should return at list one string (value). When you use capturing groups in your regex, a list would be returned. When you use the global (g) modifier, you would also return a list.

When you know the number of return values, you can receive the list with a list of scalar variables. When you do not know the number, you should receive the list with an array.

The following code receives the list of captured strings (groups) with a list of scalar variables:

use strict;

my ($var1, $var2) = ("I am a boy and you are a girl." =~ /(boy).*(girl)/);

print $var1, "\n";

print $var2, "\n";

The output is:

boy
girl

The following code is the same as the above but an array is used to receive the captured strings:

use strict;

my @arr = "I am a boy and you are a girl." =~ /(boy).*(girl)/;

print $arr[0], "\n";

print $arr[1], "\n";

The following code receives the list of globally matched strings with a list of scalar variables:

use strict;

my $subject = "A cat is an animal. A rat is an animal. A bat is a creature.";

my ($var1, $var2, $var3) = $subject =~ /[cbr]at/g;

print "$var1, $var2, $var3";

The output is:

    cat, rat, bat

The following code receives the list of globally matched strings with an array:

use strict;

my $subject = "A cat is an animal. A rat is an animal. A bat is a creature.";

my @arr = $subject =~ /[cbr]at/g;

print "$_ " foreach @arr;

The output is:

    cat rat bat

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