Perl Variable Types and Context
Perl Basics – Part 11
Perl Course
Foreword: In this part of the series, I talk about what is called Perl data types and what is also called the scalar context and the list context.
By: Chrysanthus Date Published: 29 Mar 2015
Introduction
The Data Types
Perl has three built-in data types, which are 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 I have been referring to as a simple value. Example,
my $var;
$var = "some text or a number without quotes";
You should have seen many examples of scalar in the previous parts of the series.
Meaning of an Array
An array is an ordered list of scalars. You should 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 integers. Any variable that identifies a hash begins with %. You should 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 here: 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 scalar values. 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 Perl. Try the following code:
use strict;
(my $one, my $two, my $three) = ("the first", "the second", 3);
print $one; print "\n";
print $two; print "\n";
print $three; print "\n";
You should have the three scalar values printed.
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 (try it):
use strict;
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.
Note that when you print an array as done above, the elements of the array are not separated. You should use a for-loop if you want to separate the elements.
I have mentioned 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. The parentheses for the print function can have more than one item separated by commas. In one of the previous chapters you 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 each a list. 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 Perl is working in the 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.
If any operation would return a list or would have a list as argument, we say Perl 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.
Note: The hash is a kind of list.
Context Depending on the Situation
Depending on the situation, you may be working in a scalar or list context. As you learn Perl, 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 Perl has been made to be – returning a scalar by array variable when assigned to a scalar). Try the code.
use strict;
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 Perl has been made to be – returning a list by array variable when assigned to a list). Try the code.
use strict;
my @arr = ("HBWE", "FGTR", "HTNK", 4587, 4526, 4053, "AB12", "GB58", "TG45", "RE69");
my @li = @arr;
print @li;
In the Perl 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;
my %fruitColor = (
Apple => "purple",
Banana => "yellow",
Pear => "green",
Lemon => "green"
);
my @ar = keys (%fruitColor);
print @ar;
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;
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.
You can declare more than one variable in list context as in the following statement:
my ($var1, $var2, $var3) = ("house", 25, "room");
This is equivalent to,
my $var1 = "house";
my $var2 = 25;
my $var3 = "room";
Void Context
Void means empty. If a Perl task will not produce a scalar or a list, that is a void context.
Let us end here. Remember that all the principles outlined in this series (and voulme) 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
Related Links
Perl BasicsPerl 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