Open In App

Perl | Variables and its Types

Improve
Improve
Like Article
Like
Save
Share
Report

The reserved memory locations to store values are the Variables. This means that creating a variable, reserves a space in memory. Data type of a variable helps the interpreter to allocate memory and decide what to be stored in the reserved memory. Therefore, variables can store integers, decimals, or strings with the assignment of different data types to the variables.
Perl has the following three basic data types namely – 

  • Scalars
  • Arrays
  • Hashes

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

Creating Variables

Perl variables need not to be declared explicitly to reserve memory space. Just like other programming languages, the operand to the left of the ‘=’ operator is basically the name of the variable, and the operand to the right of the ‘=’ operator is basically the value stored in the variable. 
For example: 

$age = 40; 
$name = “XYZ”; 
$rollno = 22; 
Here 40, “XYZ” and 22 are the values assigned to $age, $name and $roll no variables, respectively.   

Scalar Variables

A scalar is a single unit of data. It is possible that the data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page. Here is an example of using scalar variables  

Perl




#!/usr/bin/perl
 
# Assigning values to scalar
# variables
$age = 40;    
$name = "XYZ";
$rollno = 22;
 
# Printing variable values
print "Age = $age\n";
print "Name = $name\n";
print "Roll no = $rollno\n";


Output: 

Age = 40
Name = XYZ
Roll no = 22 
Array Variables

Variable that stores an ordered list of scalar values is of array type. Variables of Array Datatype are preceded by an “at” (@) sign. The dollar sign ($) is used to refer a single element of an array with the variable name followed by the index of the element in square brackets. 
Here is an example of how to use an array variable: 

Perl




#!/usr/bin/perl
 
# Assigning values to Array variables
@ages = (55, 80, 44);    # @ is used to declare
                        # array variables    
@names = ("XYZ", "LGH", "KMR");
 
# Printing values of Arrays
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";


Here we used ‘\’ before the ‘$’ sign just to print it as a statement. Otherwise Perl will by default understand it as a variable and will print the value stored in it. When executed, following result will be produced – 
Output: 

$ages[0] = 55
$ages[1] = 80
$ages[2] = 44
$names[0] = XYZ
$names[1] = LGH
$names[2] = KMR 
Hash Variables

A hash is a set of key/value pairs. Variables of the Hash type are preceded by a modulus (%) sign. Keys are used to refer to a single variable in the Hash. To access these elements, Hash variable name followed by the Key associated with the value is used in curly brackets. 
Following is a simple example to show Hash Variable:  

Perl




#!/usr/bin/perl
 
# Defining Hash variable using '%'
%data = ('XYZ', 55, 'LGH', 80, 'KMR', 44);
 
# Printing values of Hash variables
print "\$data{'XYZ'} = $data{'XYZ'}\n";
print "\$data{'LGH'} = $data{'LGH'}\n";
print "\$data{'KMR'} = $data{'KMR'}\n";


Output: 

$data{'XYZ'} = 55
$data{'LGH'} = 80
$data{'KMR'} = 44 
Variable Context

Based on the Context, Perl treats the same variable differently i.e., situation where a variable is being used. 
Example:  

Perl




#!/usr/bin/perl
 
# Defining Array variable
@names = ('XYZ', 'LGH', 'KMR');
 
# Assigning values of array variable
# to another array variable
@copy = @names;
 
# Assigning values of Array variable
# to a scalar variable
$size = @names;
 
# Printing the values of new variables.
print "Given names are : @copy\n";
print "Number of names are : $size\n";


Output: 

Given names are : XYZ LGH KMR
Number of names are : 3

Here @names is an array, which has been used in two different contexts. First, we copied it into any other array, i.e., list, so it returned all the elements assuming that context is list context. Next, the same array is tried to be stored in a scalar, which further returned just the number of elements in this array by default assuming it to be a scalar context. Following table displays the various contexts: 

S no. Context & Description
1. Scalar 
Assignment to a scalar variable evaluates the value on the right of ‘=’ in a scalar context. 
 
2. List 
Assignment to an array or a hash evaluates the right of ‘=’ in a list context. 
 
3. Boolean 
A boolean context is simply a place where expression is evaluated just to check whether it is true or false. 
 
4. Void 
This context doesn’t care what the return value is, even it doesn’t want a return value. 
 
5. Interpolative 
This context will only arise inside the quotes (“”), or with things that behave like quotes(“”). 
 


Last Updated : 18 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads