Broad Network

Context in Perl

Perl Data Types – Part 5

Perl Course

Foreword: In this part of the series, I talk about context in Perl.

By: Chrysanthus Date Published: 22 May 2015


This is part 5 of my series, Perl Data Types. In this part of the series, I talk about context in Perl. You should have read the previous parts of the series before reaching here, as this is a continuation. The word scalar is for single value and the word list is for plural value.

There are two major contexts in Perl: scalar and list. For an operation, the focus is either on its return value or on an argument (operand). If the focus is on its return value and if a scalar is returned, then that is scalar context. If the focus is on its return value and if a list is returned, then that is list context. If the focus is on an argument and the argument has to be a scalar, that is scalar context. If the focus is on an argument and the argument has to be a list, that is list context. There is another context, which does not occur as frequently as the scalar or the list context; it is called the void context.

Scalar Context for Return Values
In this section, I give you examples of scalar contexts for return values. Consider the following statement:

    $item = ('pen', 'book', 'ruler', 'pencil');

In this statement, we are interested in the assignment operator (operation). The right hand operand is a list and the left hand is a scalar. Our focus is on the return value. For this statement, since the left hand operand is a scalar, it is the last value of the list that is returned, to be held by $item. That is, the value of $item becomes 'pencil'. This is scalar context.

Consider the following code segment:

    for (my $i=0; $i<@articles; ++$i)
            print $articles[$i], "\n";

We are interested in the < operator in the $i<@articles expression. The right hand operand is an array while the left hand is a scalar. Since the left hand operand is a scalar, the operator returns the array length (scalar) to be compared with the value of $i. Remember, an array is not really a list; that is why the length instead of the last value is returned. An array is a variable that holds a list; an array is not an anonymous list.

Scalar Context for an Argument
Consider the syntax:

    int (EXPR)

This is the int operation that returns the integer, when the argument is a float. For example, if the argument is 2.6, it will return 2. The focus in this operation now, is not in the return value. It is in the argument. The argument, EXPR can be an expression, but the expression has to result into a single number, a scalar value. The argument must be (result in) a number; a number is an example of a scalar value, and so we say we are in scalar context.

List Context for Return Values
Consider the following statement:

    my ($a, $b, $c) = ('pen', 'book', 'ruler', 'computer', 'pencil')[1,3,4];

This is a multiple declaration of the variables, $a, $b, and $c. $a acquires the value, “book”, $b acquires the value, “computer” and $c acquires the value, “pencil”.

We are interested in the assignment operator. The right hand side returns a slice, which is assigned to an anonymous list. Here, “my” is not the name of the left hand list. I will talk about the “my” operator in a different series.

The focus here is on the return value, which is a list. We say the operation is in list context. Other examples exist.

List Context for Argument
The print command to send text to the output is actually an operator. It is not a function. The argument to the print operator is a list. It typically sends all its items to the output in the order in which the items where typed. If you want separations at the output (line), you have to type in spaces as string items for the separations. If you want the next print to go to the next line, then you have to type \n in double quotes as the last list item for the print operator.

All what you type to the right of the print operator are items of a list, some of which may serve other purposes other than just being displayed. Placing the print items in parentheses is optional. Try the following code:

use strict;

    my $item1 = "bbb";
    print "aaa", ' ', $item1, ' ', 222, "\n";

The output is:

    aaa bbb 222

The separation characters, ' ' have served their purpose. Since "\n" is the last item of the print operator, the next print will go to the next line. The print operator also replaced $item1 with its value.

The print operation operates in list context.

Void Context
Void context just means the value has been discarded. Void context does not occur frequently, so I will give details to that later.

That is it for this part of the series.


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



Become the Writer's Fan
Send the Writer a Message