Perl | Variables and its Types

The reserved memory locations to store values are the Variables. This means that creating a variable, reserves a space in memory. 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.
Perl has the following three basic data types namely –

Hence, three types of variables will be used in Perl. A scalar variable can store either a number, a string, or a reference and will precede by a dollar sign ($). An array variable will store ordered lists of scalars and precede by @ sign. The Hash variable will be used to store sets of key/value pairs and will precede by sign %.

Creating Variables

Perl variables need not to be declared explicitly to reserve memory space. Just like other programming languages, the operand to the left of the ‘=’ operator is basically the name of the variable, and the operand to the right of the ‘=’ operator is basically the value stored in the variable.
For example:

$age = 40;
$name = “XYZ”;
$rollno = 22;
Here 40, “XYZ” and 22 are the values assigned to $age, $name and $roll no variables, respectively.

Scalar Variables

A scalar is a single unit of data. It is possible that the data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page. Here is an example of using scalar variables