J2EEOnline J2EE

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.
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.