Variables are used to store a value in memory and allow to modify of its value once assigned. Memory allocation is dependent on the data type.

In Perl, Every data is stored as a Variable. Variables can be declared with Perl data types

  • scalar variables prefixed with $
  • Array variables prefixed with @
  • Hash variables prefix with %

Variables store scalar values such as integers, floating, and strings, array types with a collection of homogeneous elements, and hashes containing key and value pairs.

how to declare a variable in Perl.

Variable declaration contains Name of the variable begins with symbol, Symbol can be $, @,%based on the data and type of the data. the variable declaration contains two parts with an equal operator. The left part contains variable_name prefixed with a symbol. The right side part contains value or data.


[symbol]variable_name = value;

variable_name is a valid Perl identifier name and it is case-sensitive.
$name and $Name are two different variables. value is actual data assigned to a variable

Here is an example of variable declaration in python

How to declare scalar variables in Perl

A scalar is a single unit of data such as an integer, floating, or string. Scalar variables declared with variable name start with $ and scalar value assigned to it.




$name = "Eric" 
$id = 11

How to declare Array variables in Perl?

An array is a single variable to store a collection of a similar set of elements. Array variables declared with the variable name, prefixed with %, and list of values assigned to it.



data1 can be string, numbers, or floating.


@numbers = (20, 40, 10);
@words = ("one", "two", "three");

How to declare Hash variables in Perl?

Hash variables are used to store key and value pairs. Hash variables declared with the variable name, prefixed with % and key and value pair collection assigned to it.



data1 can be string, numbers, or floating.


%numbers = ("one", 1, "two", 2, "three", 3,"four",4)

How to change the variable value in Perl?

To modify the variable value, Please follow the below steps

  • Initially, the Variable is declared with a value. In this, the scalar variable ($value) is declared with a string value
  • modify variable value using reassigning with the new value. i.e redeclaring with new value
  • new value can be a different type of data ie integer, the previous string

The same way hash and arrays can be modified Modifying hash variable value: hash variable values can be changed with the syntax $hashvariable[key]

%numbers = ("one", 1, "two", 2, "three", 3,"four",4)
## Change one key value from 1 to 01

Modifying Array variable value: array variable values can be changed with index syntax $arrayvariable[index]

@words = ("one", "two", "three");
## Change array using index syntax

Perl allows you to modify variable value once the variable is declared and assigned with a value.

Perl Variable naming rules

  • Variable name always starts with either $, @,% symbols, followed by variable name.
  • variable names are case sensitive
  • Name is a valid Perl identifier
  • name must always starts with letter or underscore, but on a number
  • It does not contains spaces or any special characters
  • It allows only underscore(_) character

Perl Interpolation variable syntax

In Perl, Any variables(Scalar or list) included in double quotes are replaced with their value at runtime. Variables can be used as interpolation syntax. variable

$user = "Eric"
print "Hi $user, Welcome to my site!"

It prints

Hi Eric, Welcome to my site!

Variable Scopes

my: variables declared with these are accessed inside a block where it is defined.

local: Local variables stores global variable value and scope is defined inside a block. Use this in subroutine.

Perl Local Variable

Perl treat variable declaration as global variable without my keyword.

variable created with my keyword are called local variable. Local variable scope exists inside declared blocks only.

In the below example,variable $number declared inside subroutine are called local variables. The scope of the variable exists in declared sub routine.

sub printVariable {
    my  ($number ) = @_;
    print "Local: $number\n";
print "Global: $number\n";


Global: 10
Local: 20

Perl Special variables

Perl has defined inbuilt variables that has a special tasks

Inbuilt special variable  Description  
$_Stored to hold  Default input search string during iteration of arrays and hashes
$0Name of the program to execute, file name
$$Process Id
$!Current value of errno object
`@ARGV` Command line arguments
@ENVCurrent Environment information