Introduction to ActivePerl Special Variables

Basics of ActivePerl - Part 14

Chrys Forcha
Introduction
This is part 14 of my series, Basics of ActivePerl. In this part of the series, we look at what is called ActivePerl Special Variables. These are variables that have already been declared in the Interpreter for you. You do not have to declare them again. Under certain conditions, the variables acquire certain values. As you learn Perl, you learn the names of these variables, the values they acquire and the conditions under which they acquire the values. Any of these variables fall into one of the three data types. So any of these variables is a scalar or an array or a hash.

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.

Two examples
In this part of the series, we look at two of the variables, which are $_ and @_ . The former is a scalar; the later is an array. The later was mentioned in one of the previous chapters. The name of the former is _ . It is preceded by $ since it is a scalar, in the variable name. The name of the later is also _ . It is preceded by @ since it is an array. There are many such variables already declared for you in the ActivePerl interpreter.

The way you will declare your own variable, is just to begin with the reserved word, my, then a space, followed by $, @ or % and them the name of the variable, then a semicolon to make a statement. After that as you write your code your variable will acquire value (or values) depending on the code you write. These special variables behave in a similar way, with the difference that they have already been declared for you in the interpreter. Also, the conditions under which they will acquire certain values have already been determined in the interpreter. The particular values they acquire depend on the condition. You learn the conditions and the acquired values as you learn ActivePerl. In this part of the series, we learn the conditions and acquired values for $_ and @_ , at the basic level.

The names of these special variables are like punctuation signs. Some of them are actually punctuation signs, like with $?. Do not worry about $? for now. In general the names of these variables are not likely the names you would give to your own user declared variables. You normally would give a name to a variable that you can remember, like in @books, where you want books to mean an array of books. So hardly would there be conflicts between your variables and these special variables.

The $_ Variable
The syntax for the foreach construct is,

foreach Vairable (List)
{
#do something
}

If Variable (which is a scalar) in the syntax is not typed, then $_ would take its place. You can then use $_ inside the block as you would use, Variable. Consider the following code fragment:

foreach my $item (@theArr)
{
print $item, "
";
}

If you do not type the $item variable, then the special variable, $_ would take the place of the $item variable. The block of the code would have to be written as,

foreach (@theArr)
{
print $_, "
";
}

$item was not typed in the first line of the construct, so inside the block, $_ was used in its place. Read and try the following code:

use strict;

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

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

foreach (@theArr)
{
print $_, "
";
}

I hope you now appreciate the condition and values in the way $_ has behaved. There are other situations in ActivePerl where $_ is used. The situations are similar to the one just described.

The @_ Variable
When you call a subroutine (function) with arguments, when the function is being executed, all the arguments will be members of the special variable, @_ . So, within a subroutine the array @_ contains the arguments passed to that subroutine. This array variable has already been pre-declared in the interpreter. You do not have to declare it again. Whenever a subroutine is called, the values of this array become the values of the arguments passed to the subroutine. The array @_ is filled, beginning from the first index (0). If the subroutine is called without arguments, then the array will be empty. Read and try the following code:

use strict;

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

sub mySub
{
print $_[0], "
", $_[1], "
", $_[2];
}

mySub("one", "two", "three");

The subroutine is called with three arguments ("one", "two", "three"). When the subroutine is executed, the value of the first element of @_ is the value of the first argument, the value of the second element is the value of the second argument, and that of the third element is the value of the third argument.

Note: Since the name of the array, @_ , is _ , the elements of the array, are $_[0], $_[1], $_[2], etc.

Passing Lists as Arguments to Subroutines
The question you may have is this: what happens if the argument passed is an array or a hash. If the argument is an array or a hash, in the function it collapses (becomes flat) losing its identity and its items become values of the @_ array. Let us demonstrate this with an experiment.

Read and try the following code:

use strict;

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

my $scal = "one";

my @arr = ("two", "three");

my %hsh = (
DDD => "four",
EEE => "five"
);

sub mySub
{
print @_;
}

mySub($scal, @arr, %hsh);

I tried the code in my computer and I had the following as result:

onetwothreeEEEfiveDDDfour

The values are not separated by commas or spaces; let us allow things like that for now. The first argument in the call, is $scal, and its value is the first in the @_array. That is OK. The second argument is @arr. It has values "two" and "three" in that order. The second and third values in the @_ array are "two" and "three". So far as order in the arguments (scalar, then array) are concerned, the order in which the values go to the @_ array is the way they were sent.

Now the keys and the values for the hash became values of the @_ array, but not in the order in which the hash was created.

We see that even though, inside a subroutine, arrays and hashes are collapsed, the positioning of the arguments are maintained in the @_ array; the internal order for arrays are also maintained, but the internal order for the hash is not necessarily maintained. That is just how it is.

If you want the identity of arrays and hashes to be maintained, then you have to do what is called, pass-by-reference (see later). If you do that the structure of the array and hash would be maintained but there would be some limitation in your code (see later).

Let us end here and 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.