- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

2.5 Perl variable types

Perl has three main variable types: scalars, arrays, and hashes.

A scalar represents a single value:
my $animal = "camel";
my $answer = 42;
Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required. There is no need to pre-declare your variable types, but you have to declare them using the my keyword the first time you use them. (This is one of the requirements of use strict;.) Scalar values can be used in various ways:
print $animal;
print "The animal is $animal\n";
print "The square of $answer is ", $answer * $answer, "\n";
There are a number of "magic" scalars with names that look like punctuation or line noise. These special variables are used for all kinds of purposes, and are documented in 10. The only one you need to know about for now is $_ which is the "default variable". It's used as the default argument to a number of functions in Perl, and it's set implicitly by certain looping constructs.
print;          # prints contents of $_ by default
An array represents a list of values:
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed   = ("camel", 42, 1.23);
Arrays are zero-indexed. Here's how you get at elements in an array:
print $animals[0];              # prints "camel"
print $animals[1];              # prints "llama"
The special variable $#array tells you the index of the last element of an array:
print $mixed[$#mixed];       # last element, prints 1.23
You might be tempted to use $#array + 1 to tell you how many items there are in an array. Don't bother. As it happens, using @array where Perl expects to find a scalar value ("in scalar context") will give you the number of elements in the array:
if (@animals < 5) { ... }
The elements we're getting from the array start with a $ because we're getting just a single value out of the array; you ask for a scalar, you get a scalar. To get multiple values from an array:
@animals[0,1];                  # gives ("camel", "llama");
@animals[0..2];                 # gives ("camel", "llama", "owl");
@animals[1..$#animals];         # gives all except the first element
This is called an "array slice". You can do various useful things to lists:
my @sorted    = sort @animals;
my @backwards = reverse @numbers;
There are a couple of special arrays too, such as @ARGV (the command line arguments to your script) and @_ (the arguments passed to a subroutine). These are documented in 10.
A hash represents a set of key/value pairs:
my %fruit_color = ("apple", "red", "banana", "yellow");
You can use whitespace and the => operator to lay them out more nicely:
my %fruit_color = (
    apple  => "red",
    banana => "yellow",
To get at hash elements:
$fruit_color{"apple"};           # gives "red"
You can get at lists of keys and values with keys() and values().
my @fruits = keys %fruit_colors;
my @colors = values %fruit_colors;
Hashes have no particular internal order, though you can sort the keys and loop through them. Just like special scalars and arrays, there are also special hashes. The most well known of these is %ENV which contains environment variables. Read all about it (and other special variables) in 10.

Scalars, arrays and hashes are documented more fully in 5.

More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes.

A reference is a scalar value and can refer to any other Perl data type. So by storing a reference as the value of an array or hash element, you can easily create lists and hashes within lists and hashes. The following example shows a 2 level hash of hash structure using anonymous hash references.

my $variables = {
    scalar  =>  {
                 description => "single item",
                 sigil => '$',
    array   =>  {
                 description => "ordered list of items",
                 sigil => '@',
    hash    =>  {
                 description => "key/value pairs",
                 sigil => '%',
print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n";

Exhaustive information on the topic of references can be found in "Mark's very short tutorial about references" (perlreftut) in Perl Tutorials, "Manipulating Arrays of Arrays in Perl" (perllol) in Perl Tutorials, 15 and "Perl Data Structures Cookbook" (perldsc) in Perl Tutorials.

ISBN 9781906966027Perl Language Reference ManualSee the print edition