Perl Variables  «Prev  Next»
Lesson 1

Introduction to Perl Variables

Perl Variable Concept
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.

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.