Open In App

Perl | Variables

Variables in Perl are used to store and manipulate data throughout the program. When a variable is created it occupies memory space. The 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. 

Naming of a Variable

A variable in Perl can be named anything with the use of a specific datatype. There are some rules to follow while naming a variable: 



Example: 

$John and $john are two different variables

Example:



$my-name = "John"; // Invalid 
$my name = "John"; // Invalid
$my_name = "John"; // Valid
Declaration of a Variable

  
Variable Declaration is done on the basis of the datatype used to define the variable. These variables can be of three different datatypes: 
 

Syntax: $var_name = value;

Example: 

$item = "Hello" 
$item_one = 2

Syntax : @var_name = (val1, val2, val3, …..);

Example: 

@price_list = (70, 30, 40);
@name_list = ("Apple", "Banana", "Guava");

Syntax : %var_name = ( key1=>val1, key2=>val2, key3=>val3, …..);

Example: 

%item_pairs = ("Apple" =>2, "Banana'=>3);
%pair_random = ("Hi" =>8, "Bye"=>9);
Modification of a Variable

  
Perl allows modifying its variable values anytime after the variable declaration is done. There are various ways for the modification of a variable:
 

Example: 

$name = "John";

# This can be modified by simply
# redeclaring the variable $name.
$name = "Rahul";

Example: 

@array = ("A", "B", "C", "D", "E");

# If value of second variable is to
# be modified then it can be done by
@array[2] = "4";
# $array[2]  = "4"; is an alternate way of updating value in an array.

# This will change the array to,
# @array = ("A", "B", "4", "D", "E");

Example: 

%Hash = ("A", 10, "B", 20, "C", 30)

# This will modify the value 
# assigned to Key 'B'
$Hash{"B"} = 46;
Variable Interpolation

  
Perl provides various methods to define a String to a variable. This can be done with the use of single quotes, double quotes, using q-operator and double-q operator, etc. 
Using single quotes and double quotes for writing strings is the same but there exists a slight difference between how they work. Strings that are written with the use of single quotes display the content written within it exactly as it is. 
 

Example:

$name = "John"
print 'Hi $name\nHow are you?' 

The above code will print:

Hi $name\nHow are you?

Whereas strings written within double quotes replace the variables with their value and then display the string. It even replaces the escape sequences with their real use.
Example: 

$name = "John"
print "Hi $name\nHow are you?" 

The above code will print: 

Hi John
How are you?

Example Code: 




#!/usr/bin/perl
use Data::Dumper;
 
# Scalar Variable
$name = "GeeksForGeeks";
 
# Array Variable
@array = ("G", "E", "E", "K", "S");
 
# Hash Variable
%Hash = ('Welcome', 10, 'to', 20, 'Geeks', 40);
 
# Variable Modification
@array[2] = "F";
 
print "Modified Array is @array\n";
 
# Interpolation of a Variable
 
# Using Single Quote
print 'Name is $name\n';
 
# Using Double Quotes
print "\nName is $name";
 
# Printing hash contents
print Dumper(\%Hash);

Output: 
Modified Array is G E F K S
Name is $name\n
Name is GeeksForGeeks$VAR1 = {
          'to' => 20,
          'Welcome' => 10,
          'Geeks' => 40
        };

 


Article Tags :