ActivePerl Variable Types and Context

Basics of ActivePerl - Part 11

Chrys Forcha
Introduction
This is part 11 of my series, Basics of ActivePerl. In this part of the series, we look at what is called ActivePerl data types and what is also called the scalar context and the list context.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Data Types
Perl has three built-in data types, which are called the scalars, arrays and hashes.

Meaning of a Scalar
In simple terms, a scalar is a string or a number. Any variable that identifies a scalar begins with $. A scalar is what we have been referring to as a simple value. Example,

my $var;
$var = "some text or a number without quotes";

We have seen many examples of scalar.

Meaning of an Array
An array is an ordered list of scalars. We have seen examples of this. Any variable that identifies an array must begin with @.

Meaning of Hash
A hash is an unordered list of scalars. One main difference between a hash and an array is that the values of the hash are indexed by associated string keys, while the values of an array are indexed by numbers. Any variable that identifies a hash begins with %. We have seen examples of hashes.

Valid variable Name
From the above, we see that a variable begins with either, $, @ or %. After that you should have a letter or underscore. After that, you can have any number of underscores, letters or digits in any order, to form the variable name.

List
A list is a collection of scalars separated by commas, delimited by parentheses. An example is:

("the first", "the second", 3)

There are three scalars there: two strings and one number.

You can have lists on both sides of the assignment operator. Consider the following line:

(my $one, my $two, my $three) = ("the first", "the second", 3);

There are two lists here: one on the left of the assignment operator and the other on the right of the assignment operator. I intentionally made both lists to have three scalars each. The list on the right has real scalars. The one on the left has but variables. Each of the variables for the list on the left, will hold the corresponding scalar from the list on the right. I hope you are appreciating the meaning of list in ActivePerl. Read and try the following code:

use strict;

print "Content-Type: text/html\n\n";

(my $one, my $two, my $three) = ("the first", "the second", 3);

print $one; print "
";
print $two; print "
";
print $three; print "
";

You should have the three scalar values displayed.

A list can be assigned to another list with corresponding variables as shown above. However, a list can also be assigned to an array as shown in the following code (read and try it):

use strict;

print "Content-Type: text/html\n\n";

my @arr = ("the first", "the second", 3);

print @arr;

This method of assigning a list is preferred, when you do not know the number of elements in the list, or when the list is very long.

Arguments to a Function
We have seen the print function. In the first code sample above, we have

print $one;

It can also be "print ($one);" but the brackets are usually omitted. In one of the previous chapters we saw something like:

push (@hisArr, ("xxx", "yyy", "zzz"));

The is the push function for the array. It also has brackets. The brackets have items inside. Here I have mentioned the print and push functions. All what you have inside the brackets of a function as in the above two cases, are called Arguments. In the case of print above, the argument is $one, which is a scalar. In the case of push the arguments are @hisArr and ("xxx", "yyy", "zzz"). These two arguments are lists. An array is an ordered list, and that is the first argument. The second argument is a literal list. The outermost brackets for the arguments of a function can be omitted.

Scalar Context
If any operation would return a scalar or would have a scalar as argument, we say ActivePerl is working in a scalar context at that point. Consider the following example:

my $var = "the string";

This is a very simple statement and it is scalar context, as we are dealing with scalars on either side of the assignment operator. Consider the following.

print ($one);

The argument of the print function is a scalar, so that is a scalar context.

List Context
If any operation would return a list or would have a list as argument, we say ActivePerl is working in a list context at that point. Consider the following example:

(my $one, my $two, my $three) = ("the first", "the second", 3);

On either side of the assignment operator, we have a list, so we have a list context. Consider the following:

push (@hisArr, ("xxx", "yyy", "zzz"));

The two arguments of the push function are lists, so we have a list context. In some cases the arguments of a functions may be made up of scalars and list. In that case ActivePerl is working on both contexts.

Note: The hash is a kind of list.

Conditional Context
Depending on the condition, you may be working in a scalar or list context. As you learn Activeperl, you are told these conditions. I will give just a few examples here.

Consider the following array:

my @arr = ("HBWE", "FGTR", "HTNK", 4587, 4526, 4053, "AB12", "GB58", "TG45", "RE69");

After the array has been created, its array variable would behave as an operation and would return either the length (scalar) of the array or a list of the elements of the array, depending on whether it is assigned to a scalar or an array (list). When it returns a scalar, you are working in a scalar context. When it returns a list, you are working in a list context.

In the following code the array variable is assigned to a scalar, so it returns the length of the array (that is how ActivePerl has been made to be - returning a scalar by array variable when assigned to a scalar). Read and try the code.

use strict;

print "Content-Type: text/html\n\n";

my @arr = ("HBWE", "FGTR", "HTNK", 4587, 4526, 4053, "AB12", "GB58", "TG45", "RE69");

my $scal = @arr;

print $scal;

In the following code the array variable is assigned to a list, so it returns the list of elements in the array (that is how ActivePerl has been made to be). Read and try the code.

use strict;

print "Content-Type: text/html\n\n";

my @arr = ("HBWE", "FGTR", "HTNK", 4587, 4526, 4053, "AB12", "GB58", "TG45", "RE69");

my @li = @arr;

print @li;

The Hash keys and values functions revisited
In the Perl or ActivePerl specification, it is said that the hash keys function would return a list of all the keys in the hash if working in the list context or it would return the number of keys in the hash if working in scalar context. So in list context it returns a list; in scalar context it returns a scalar (number). This means that if you assign the return value of the function to an array, you have a list; if you assign it to a scalar you have a scalar.

Read and try the following code where the keys function is in list context, returning a list because the returned value is assigned to an array.

use strict;

print "Content-Type: text/html\n\n";

my %fruitColor = (
Apple => "purple",
Banana => "yellow",
Pear => "green",
Lemon => "green"
);

my @ar = keys (%fruitColor);
print @ar;

Read and try the following code where the keys function is in scalar context, returning a scalar because the returned value is assigned to a scalar.

use strict;

print "Content-Type: text/html\n\n";

my %fruitColor = (
Apple => "purple",
Banana => "yellow",
Pear => "green",
Lemon => "green"
);

my $num = keys (%fruitColor);
print $num;

Always remember that a scalar begins with $ and an array begins with @, as you decide on the variables that receive the returned values.

The hash values function works in a similar way, returning the list of values in the hash, in list context or the number of values in the hash in scalar context. At this point you should be able to write two code samples to demonstrate that.

Let us end here. Remember that all the principles outlined in this series are applicable, without any modification to traditional Perl. Just begin your traditional Perl program with something like #!/usr/bin/perl .

We continue in the next part of the series.

Chrys

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.