| Variable Data Types |
|---|
There are three basic variable data types in Perl: scalars, arrays, and hashes.
Scalars are single numbers or strings, delineated by a dollar sign ($).
Example:
$data, $this_number
Arrays contain two or more scalar data elements. An array is denoted by the "at" sign (@). The term 'array' in Perl really refers to a scalar array, or list.
Example:
@desk_items = ("pencil", "pen", "calculator", "keyboard", "coffeepot");
Shortcut: Perl allows you to put quotes around individual array elements using the qw construct. The above array declaration then becomes
@desk_items = qw(pencil pen calculator keyboard coffeepot);
The parentheses aren't necessary; (almost) any character can be used as a delimiter.
@desk_items = qw/pencil pen calculator keyboard coffeepot/;
is just as valid.
Array elements are accessed using the dollar sign ($) and square brackets ([ ]), with the first array element being 0 by default:
$desk_items[1] = "pen";
Multidimensional arrays do not exist as such in Perl. However, since a multidimensional array (also called a matrix) is in effect an array of lists, you can make use of Perl's anonymous array to do the same job (it's called an anonymous array because you don't assign a scalar reference to it).
Example:
@dogs=("toy", [chihuahua, yorkie, peke],
"miniature", [dachshund, poodle, beagle],
"standard", [lab, afghan, dane]
);
To dereference the anonymous array elements, use the arrow (infix) operator ->:
print "My dog is a $dogs[1] $dogs[1]->[0].\n";
This should produce the following output:
My dog is a miniature dachshund.
Hashes (also called associative arrays) are pairs of scalars referred to as keys and values. Obviously, hashes should contain even numbers of scalars, because each key must have a value and vice-versa (however, if Perl encounters a key with no corresponding value, it simply regards that value as undefined). Hashes are denoted by the "percent" sign (%).
Example:
%dogs=('T','toy','M','miniature','S','standard');
In this example, T, M, and S are the keys; toy, miniature, and standard are the values.
For the sake of clarity and ease of reference, Perl provides an alternate notation system for hash elements that uses the arrow operator =>:
%dogs=(
'T'=>'toy',
'M'=>'miniature',
'S'=>'standard'
);
Hash elements are accessed using curly braces ({ }):
print "My dog is a $dogs{'M'} dachshund\n";
Which produces the output
My dog is a miniature dachshund
In much the same way that anonymous arrays can be used to provide multidimensionality for lists, the anonymous hash may be employed to nest hashes.
Example:
%pets=(
'dog' => {
'toy' => 'peke',
'miniature' => 'dachshund',
'standard' => 'lab'
},
'cat' => {
'shorthair' => 'tabby'
'longhair' => 'persian',
'curlyhair' => 'rex'
}
);
To dereference nested hash elements, use the infix operator ->:
print "My pet is a $pets{'cat'}->{'shorthair'} cat\n";
Which produces
My pet is a tabby cat
Finally, you can also construct an array of anonymous hashes:
@pets=(
{
'type'=>'dog',
'breed'=>'dachshund',
'color'=>'black and tan'
},
{
'type'=>'cat',
'breed'=>'tabby',
'color'=>'orange'
}
);
Access nested hash elements in this array using a combination of the syntaxes for arrays and hashes:
print "My pets are a $pets[0]{'color'} $pets[0]{'breed'} $pets[0]{'type'}
and an $pets[1]{'color'} $pets[1]{'breed'} $pets[1]{'type'}\n";
This will produce
My pets are a black and tan dachshund dog and an orange tabby cat
Created by Robert G. Ferrell