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.

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 two parts with an equal operator. The left part contains variable_name prefixed with a symbol. Symbol can be $, @,%based on the data 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 new value
  • new value can be a different type of data ie integer, the previous string
$valu=11

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
  • 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!