Perl Variables

Variables are nothing but reserved memory locations to store values. This means that 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.

We have learnt that Perl has following three basic data types:

Scalars

Arrays

Hashes

Accordingly we are going to use three types of variables in Perl. A scalar variable will precede by a dollar sign ($) and it can store either a number, a string, or a reference. A array variable will precede by sign @ and it will store ordered lists of scalars. Finaly Hash variable will precede by sign % and will be used to store sets of key/value pairs.

Perl maintains every variable type in a separate namespace. So you can, without fear of conflict, use the same name for a scalar variable, an array, or a hash. This means that $foo and @foo are two different variables.

Creating Variables

Perl variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.

Keep a note that this is mandatory to declare a varibale before we use it if we use use strict statement in our program.

The operand to the left of the = operator is the name of the variable, and the operand to the right of the = operator is the value stored in the variable. For example:

Here 25, "John Paul" and 1445.50 are the values assigned to $age, $name and $salary variables, respectively. Shortly we will see how we can assign values to arrays and hashes.

Scalar Variables

A scalar is a single unit of data. That data might be a integer number, floating point, a character, a string, a paragraph, or an entire web page. Simply saying it could be anything, but only a single thing.

Array Variables

An array is a variable that stores an ordered list of scalar values. Array variables are preceded by an "at" (@) sign. To refer to a single element of an array, you will use the dollar sign ($) with the variable name followed by the index of the element in square brackets.

Hash Variables

A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name followed by the "key" associated with the value in curly brackets.

Here @names is an array, which has been used in two different contexts. First we copied it into anyother array ie. list so it returned all the elements assuming that context is list context. Next we used same array and tried to store this array in a scalar, so in this case it returned just number of elements in this array assuming that context is scalar context. Following table lists down various contexts:

S.N.

Context and Description

1

Scalar:Assignment to a scalar variable evaluates the right-hand side in a scalar context.

2

List:Assignment to an array or a hash evaluates the right-hand side in a list context.

3

Boolean:Boolean context is simply any place where an expression is being evaluated to see whether it's true or false

4

Void:This context not only doesn't care what the return value is, it doesn't even want a return value.

5

Interpolative:This context only happens inside quotes, or things that work like quotes.