Skip to main content

Perl Variable

Variable is an very important part of every modern programming language because variable is capable to hold value into in memory and this value can be access by name of variable. Variable is store a single value and this can hold the reference to other variable. That is a simple overview of variable but variable is more powerful.

Type of variables

There are three types of variable are possible in perl programming which are store the value of scalar, hash and list elements.

Scalar Variables

In perl programming all scalar variable are start with $ symbol. Scalar variable is capable to contain the values of numbers and string values. Some of cases $ can use to create a class object and that can be hold the reference of other variables.

package Regular;

#Define string variable
#Assigning string literals
$name = "Virat"; 

#Assign number
$age = 21; 

#Referencing to a scalar
$ref = \$age; 

print ("name : $name \n");
print ("age  : $age \n");

#here $$ are used to dereference
print ("ref  : $$ref \n");
Perl Scalar Variables
name : Virat 
age  : 21 
ref  : 21 

In perl programming \$varName are returns the reference of variable. In this example there are three type of scalar variable are define. $age variable is containing a number value which is 21. $name are contains a string literal value. and interesting are third variable $ref, Which is contains a reference to other variable.

When you are printing the value $ref variable then it will display something address because this variable are storing the address of $age variable. And using of $ prefix of $ref are using the actual value of $age.

We can declare multiple scalar variable into a single statement in this way.

use warnings;
use strict;

my ($num1,$num2) = (10,20);

print $num1,"+",$num2,"=",($num1+$num2);

Array Variables

Array is collection of list of elements, array variables are create in perl programming by using of a @ sign (symbol). The main advantage of array it can store multiple values in order sequence and this element are used by array variable name and its particular element index. Here is an example.

package Regular;

@country = ("India","Russia","China","United States");

#Get array element by index
print $country[0],"\n";
print $country[1],"\n";

print $country[2],"\n";
print $country[3],"\n";

print "-----------\n";
#Get array element by negative index

print $country[-1],"\n";
print $country[-2],"\n";
print $country[-3],"\n";
print $country[-4],"\n";
Perl array variable
United States
United States

Array elements are start with zero, and increment the index value one by one upto the last element of array. And That element can be also used by negative index value which is start with -1. Array element can be used as a scalar prefix by $ sign. Here given a small overview of Perl Array.

Hash Variables

Hash variable are declared by using of %-sign. hash are contain key and values pair make a single element. key of the hash are defined by programer and each key of an hash is can be form of any scalar value. And two similar keys can not possible into a single hash.

package Regular;
#define hash array
%colors = (

print $colors{"white"},"\n";
print $colors{"brown"},"\n";
print $colors{"blue"},"\n";
print "----------\n";

#print key and values
print %colors{"white"},"\n";
print %colors{"black"},"\n";
print %colors{"blue"},"\n";
Create Hash variable


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment