Mike Gossland's Perl Tutorial Course for Windows

What's Perl | Task Examples | Running Perl | Variables | Flow Control | Simple Scripts

Chapter 1: Introduction 

Variables and Types

This section introduces the idea of variables. Perl uses variables to represent things that can take different values, kind of like high school algebra where you let x represent an unknown. Variables are automatically of a certain type; here we'll just talk about the three main types: scalars, arrays, and hashes.

Scalars. A scalar is a single piece of information. Scalars can hold numbers or text. Scalars are given names (just like the unknown x is in algebra) but these names always must start with a $ sign (for scalar). The name may not contain spaces but may have many letters. The following are all scalar variables with a value assigned:

$x = 10
$value = $x + 1
$number_of_items = 15
$word = "hello"
$text = "This is a sentence but is still a scalar"

Arrays. Arrays hold multiple pieces of information that can all be referrred to at once. Arrays can hold numbers or text and their names always start with a @ sign (for array). They can have as many values within them as you'd like. Here are some examples of arrays:

@array = ( 1, 2 )
@words = ( "first", "second", "third" )
@values = ( $x, $y, 3, 5)

Once you've got an array, you can get the values from it by referring to the element in the list with a subscript in square parentheses. Subscripts start at 0, not 1. Notice that since you are extracting a single value out of it, you are referring to a scalar, and therefore you change the prefix to a $. Given the examples above:

$array[0] has the value 1
$array[1] has the value 2
$words[0] has the value "first"

and so on.

You should also know that Perl can do assignments to individual variables simultaneously if they are put into a "list context", i.e., put into parentheses. So

($x, $y, $z ) = ( 1, 2, 3);

would assign the values 1, 2, 3 to $x, $y, $z respectively. This "list context" idea comes up frequently in Perl.

Hashes. Hashes a really great part of perl, and they are extremely useful in practice. Hashes are just special arrays. They are special because instead of having numerical indexes into the elements, like [0], they have words as the indexes. They are also written in a slightly different way, with curly braces instead of square brackets. The curly braces suggest that they are fancy arrays.

To make a hash element, you just define it, using a key and value pair:

$servings{pizza} = 30;
$servings{coke} = 40;
$servings{spumoni} = 12;

The keys above are "pizza", "coke", and "spumoni", and the values are 30, 40, and 12. You could use strings for values too:

$occupation{Jeff} = "manager";
$occupation{Martha} = "interior designer";

Here the keys are Jeff and Martha, and the values are manager and interior designer.

If you want to refer to the hash itself, you use a % sign, so these hashes would be %servings and %occupation.

You will see hashes used a great deal in other people's Perl scripts and you will find many uses for them yourself too.

The default variable. Perl provides a default variable designated with an underscore symbol: $_.  Just as this name suggests, it is a scalar variable with the name "underscore". This variable is used whenever a variable is required, but where you're too lazy to bother specifying one.

As a specific example, suppose this default variable, $_, has the value of "hello world". Since the print statement normally requires a variable to print, if you leave it out the default variable will be used. So if you write:


then you'll get

hello world

Nifty, huh? This is part of perl culture. Once you know what's going on, you can use fewer words to get more done.