Perl Variables  «Prev  Next»
Lesson 1

Introduction to Perl Variables

This module will discuss the basic vocabulary of Perl, namely its variable types.
This will allow you to write simple scripts and, more importantly, it prepares you to go on to learn the operators and other syntax you need to really use the language effectively. Like most languages, Perl categorizes data of different types and requires that they be represented with specific syntax. This module will teach you about those different types of data and how to represent them.

Perl Variable Concepts

A variable is like a pigeon hole for data. It's where the data lives when it's not busy being data. Most programming languages have different types of variables for different types of data. But, Perl is a little different in this respect.
In Perl, all data types are based on one simple type called a scalar. Scalar data can be either text or numeric and sometimes both. This simplification makes its possible for you to write your programs without worrying about what data type a particular variable is.
More complex data is built by aggregating scalars into other structures like arrays and hashes. These aggregations are convenient at times, and allow you to write more complex programs without writing more complex programs.
Listing 1 is a short Perl program that shows how a simple program may look.
LISTING 1: Hello, World!
use strict;
use warnings;
use diagnostics;
# this is a comment
print "Hello, World!\n"; # so is this

Perl Scalar Variables

Scalar variables have a leading $ for example, these are all valid Perl scalar variables:
$n $N 

Case matters in Perl, so the first two examples are different variables. The final variable ''$'' is special, one of many that the Perl interpreter will use by default in various operations if the programmer does not indicate otherwise. Any particular valid identifier can be used to designate a scalar, an array, and a hash. The leading character determines which of the three types the variable has. For example, here the identifier name is used to denote three different variables:
$name @name %name

  1. The first is a scalar;
  2. the second is an array;
  3. the last is a hash.
All three can be used concurrently, as they denote different storage areas. A variable of the type scalar can contain any scalar value:
$v1 = 'good morning';
$v1 = 127;

The first assignment places a string value in the variable. The second replaces the string with an integer value. This is different from many strongly typed languages (such as C++ and Java), where types are finely divided into categories such as integer, real, string, and boolean. In Perl these are values, but not types; Perl uses type distinction mainly to separate singular entities (scalar) from collective entities (arrays and hashes).
String values are delimited with either single or double quotes. Single-quoted literals are used exactly as written, whereas double-quoted literals are subject to escape character and variable interpolation before the final value is obtained. For example:
$numer = 2;
$st1 = 'one fine $numer day';
$st2 = "$numer fine day \n";
print $st2;
print "$st1\n";

The output from this script is
2 fine day
one fine $numer day


Four interpolations have taken place. In the second line, the $numer appears to be a use of a variable, but because the string is in single quotes the characters are included as they appear. In the third line the string literal is in double quotes, so the variable name is replaced with its current value (2) to produce the final string value;
the escape sequence ''\n" also puts in a newline character. The first print statement shows the result. The second print shows two interpolations as well, as the string being printed is in double quotes. The value in $st1 is interpolated into the output string and then a newline is added to the end. Even though $st1 has ''$numer" in its value, this is not recursively interpolated when those characters are put into the output string.

Learning a new language is difficult, partly because you sometimes must use the language to explain the language.
There are examples in this course that use language elements that have not yet been thoroughly explained.
We will presume that you have already studied Learning Perl, using at least the fifth edition, or at least pretend you have, and that you have played enough with Perl to already have those basics down. Make sure you know the following things.
  1. How to run a Perl program on your system
  2. The three basic Perl variable types: scalars, arrays, and hashes
  3. Control structures such as while, if, for, and foreach
  4. Subroutines
  5. Basic regular expressions
  6. List operators such as grep, map, sort, and print
  7. File manipulation such as open, file reading, and -X (file tests)
You might pick up deeper insight into these topics in this course, but we are going to presume you know the basics.
The final parts of this course deal with distributions and contributing to CPAN.

Beginning Perl Programming
Variables are nothing but reserved memory locations to store values. When you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory.
Therefore, by assigning different data types to variables, you can store integers, decimals, or strings in these variables.
Perl has following three basic data types:
  1. Scalars
  2. Arrays
  3. Hashes
Accordingly we are going to use three types of variables in Perl.
A scalar variable will be preceded by a dollar sign ($) and it can store either a number, a string, or a reference.
An array variable will be preceded by the @ symbol and it will store ordered lists of scalars.
The hash variable will be preceded by the % symbol and will be used to store sets of key/value pairs.
Perl maintains every variable type in a separate namespace. You can use the same name for a scalar variable, an array, or a hash. This means that $foo and @foo are two different variables.

Basic Data Types and Values

Perl provides three types of data for programmers to manipulate: scalar, array, and hash (associative array). Scalar types are well known to most programmers, as is the array type. The hash is less well known and one of the most powerful aspects of Perl. Scalar values include integer, real, string, and boolean, with the expected operations available for each. Variables do not have to be declared before use; the first character indicates the type.

Advanced Perl Programming