Easy Tutorial
❮ Perl References Perl Date Time ❯

Perl Variables

Variables are data stored in memory, and creating a variable will allocate space in memory.

The interpreter determines the storage space in memory based on the variable's type, allowing you to assign different data types to variables, such as integers, floats, strings, etc.

In the previous chapter, we introduced the three basic data types in Perl: scalars, arrays, and hashes.


Perl sets up separate namespaces for each variable type, so variables of different types can use the same name without conflicts. For example, $foo and @foo are two different variables.


Creating Variables

Variables do not require explicit type declaration; the interpreter automatically allocates the appropriate type space after variable assignment.

Variables are assigned using the equals sign (=).

>

We can use the use strict statement in our program to force all variables to be declared.

The left side of the equals sign is the variable, and the right side is the value. Here is an example:

$age = 25;             # Integer
$name = "tutorialpro"; # String
$salary = 1445.50;     # Float

In the above code, 25, "tutorialpro", and 1445.50 are assigned to the variables $age, $name, and $salary, respectively.

Next, we will see the usage of arrays and hashes.


Scalar Variables

A scalar is a single unit of data. The data can be an integer, float, character, string, paragraph, etc. In simple terms, it can be anything. Here is a simple example of scalars:

Example

#!/usr/bin/perl

$age = 25;             # Integer
$name = "tutorialpro"; # String
$salary = 1445.50;     # Float

print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";

The output of the above program is:

Age = 25
Name = tutorialpro
Salary = 1445.5

Array Variables

An array is a variable used to store an ordered list of scalar values.

Arrays start with the @ symbol.

To access array variables, you can use the dollar sign ($) + variable name and specify the index, as shown in the example below:

Example

#!/usr/bin/perl

@ages = (25, 30, 40);             
@names = ("google", "tutorialpro", "taobao");

print "\$ages[0] = $ages[0]\n";
print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";

The output of the above program is:

$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = google
$names[1] = tutorialpro
$names[2] = taobao

In the program, we used the escape character (\) before the $ sign to output the $ character.


Hash Variables

A hash is a collection of key/value pairs.

Hashes start with the % symbol.

To access hash values, you can use the $ + {key} format:

Example

#!/usr/bin/perl

%data = ('google', 45, 'tutorialpro', 30, 'taobao', 40);

print "\$data{'google'} = $data{'google'}\n";
print "\$data{'tutorialpro'} = $data{'tutorialpro'}\n";
print "\$data{'taobao'} = $data{'taobao'}\n";

The output of the above program is:

$data{'google'} = 45
$data{'tutorialpro'} = 30
$data{'taobao'} = 40

Variable Context

The context refers to the position of the expression.

The context is determined by the type of variable on the left side of the equals sign. If it's a scalar, it's a scalar context; if it's a list, it's a list context.

The Perl interpreter determines the type of variable based on the context. Here is an example:

Example

#!/usr/bin/perl

@names = ('google', 'tutorialpro', 'taobao');

@copy = @names;   # Copy the array
$size = @names;   # Assigning an array to a scalar returns the number of elements
print "Names are: @copy\n";
print "Number of names: $size\n";

The above program execution output is:

Names are: google tutorialpro taobao
Number of names: 3

In the code, @names is an array that is used in two different contexts. The first context copies it to another array, so it outputs all the elements of the array. The second context assigns the array to a scalar, which returns the number of elements in the array.

The following lists various contexts:

Number Context and Description
1 Scalar - Assigned to a scalar variable, evaluated in scalar context on the right side
2 List - Assigned to an array or hash, evaluated in list context on the right side
3 Boolean - Boolean context is a simple expression evaluation to check if it is true or false
4 Void - This context does not require what value is returned, generally no return value is needed
5 Interpolation - This context occurs only within quotes
❮ Perl References Perl Date Time ❯