Variables are used to store a value in memory and allow modifying 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.

The variable declaration contains the Name of the variable that begins with a symbol, The 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.

Syntax:

[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.

Syntax:

$variable_name=data

Example:

$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.

Syntax:

@variable_name=(data1,data2,..dataN)

data1 can be string, numbers, or floating.

Example:

@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.

Syntax:

%variable_name=(data1,data2,..dataN)

data1 can be string, numbers, or floating.

Example:

%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.
$value="eric"
  • Modify variable value using reassigning with the new value. i.e. redeclaring with a new value can be a different type of data i.e. integer, the previous string
$valu=11

In 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
$numbers["one"]=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
$words[2]="Two"

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 start with a letter or underscore, but on a number
  • It does not contain 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 store global variable values and scope is defined inside a block. Use this in the subroutine.

Perl Local Variable

Perl treats variable declaration as a global variable without the my keyword.

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

In the below example, the variable $number declared inside the subroutine is called a local variable. The scope of the variable exists in the declared subroutine.

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

Output:

Global: 10
Local: 20

Perl Special Variables

Perl has defined inbuilt variables that have a special tasks

Inbuilt special variableDescription
$_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