Updated March 31, 2023
Definition of Perl Variables
Perl variables are the variables that is used to reserved memory to store values of declared variables, it means that at the time of creation or declaration of a variable in Perl we reserve some space of memory to this variable. The memory allocation is based on which data type we have used at the time to create new variables. We can assign a string, integer and decimal data types to the variables on that basis memory are allocated to the variables in Perl. Variables are the most important and useful in Perl.
How to Declare Perl Variables?
The examples are as follows:
- There are three basic data types available in perl. Using this type we have to declare the variable as per our requirement.
- If we want to declare a single variable we need to use my package name before the declaration of variables.
- There are three types of variables available, the declaration of every type is different as per variables.
- We can declare an integer type of variables is as follows. Below is the example of to declare integer type of variables is as follows. Integer comes under in scalar types.
- My $age = 20; # this is integer type variable declaration.
- If we want to declare a string or character then we have used the scalar variable as string type in perl.
- My $name = “ABC”; # this is the string type variable declaration.
- If we want to declare a floating-point number then we have used the scalar variable as a floating-point number in perl.
- my $salary = 10000.25; # this is the floating-point type variable declaration.
- If we want to declare an array and assign single variable of an array then we have used an array variable in perl. Below is the example of array variable declaration:
- @ages = (20, 25, 30, 45, 40); # integer type variable array declaration.
- @names = (‘ABC’, ‘PQR’, ‘XYZ’); # String type variable array declaration.
- @salary = (10.1, 10.2, 10.3, 10.4, 10.5); # Floating point type variable array declaration.
Below is the example of declaring hash variables:
- Hash variable declaration starts with a % sign. %data = (‘AB’, 25, ‘PQ’, 30, ‘XY’, 35, ‘AB’, 45, ‘PQ’, 40);
How to Initialize?
Below are the example to initialize.
- We can initialize variables as per the types of variables that we have used. Below is the example of initializing the scalar type variable are as follows.
- We can initialize variables in single as well as in multiple lines. We have taken the example of below variable declaration.
my $age = 25;
my $name = AB; # Declaration
my $salary = 1000.25;
- Multiple line variable initialization –
{
print "$age\n";
print "$name\n";
print "$salary\n";
}
- Single line variable initialization –
print "\n $age, $name\n, $salary\n";
- In the above example, we have initialized variables in a single line as well as in multiple lines. The output of variable initialization is the same using single as well as multiple line initialization in Perl.
Types of Perl Variables with Example
The variables are basically of three types.
- Scalars
- Arrays
- Hash
There are different types of notations used in perl to declare and initialize a variable. Scalar is used to declare integer or string and it starts or precedes with a dollar ($) sign in Perl. We have defined the type of variables as per record which type of data we have used in a program.
1. Scalar
- The below example shows a scalar type variable in perl. In the below example we have declared three variable age as an integer type, name as string type and salary as floating-point type.
- Scalar type variable is started with my package name and precedes with a dollar ($) sign.
- Scalar variables state that a single unit data.
Example
my $age = 20; # this is integer type variable declaration.
my $name = ABC; # this is the string type variable declaration.
my $salary = 10000.25; # this is the floating point type variable declaration.
use warnings;
if (1<2)
{
print "$age\n";
print "$name\n";
print "$salary\n";
}
Output:
2. Arrays
- Array variable in perl is nothing but an ordered list of scalar variables. Array variables in perl start with @ sign.
- In the below example, we have declared one variable as five value of ages and one variable as three value of names.
- Initialization of an array variable we need to use a $ sign to display a single element of array variables.
- Below is the example of an array variable in perl is as follows.
Example
@ages = (20, 25, 30, 35, 40); # integer type variable array declaration.
@names = ('ABC', 'PQR', 'XYZ'); # String type variable array declaration.
use warnings;
if (1<2)
{
print "\$ages[0] = $ages[0]\n";
print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";
print "\$ages[3] = $ages[3]\n";
print "\$ages[4] = $ages[4]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";
}
Output:
3. Hash
- The hash variable in Perl nothing but a set of key or value pairs. Hash variables in Perl start with a % sign.
- In the below example, we have declared key pairs of names and ages in data variables.
- Initialization of a hash variable we need to use a $ sign to display a single element of hash variables.
Below is the example of a hash variable:
Example
%data = ('ABC', 25, 'PQR', 30, 'XYZ', 35, 'ABC', 45, 'PQR', 40);
use warnings;
if (1<2)
{
print "\$data{'ABC'} = $data{'ABC'}\n";
print "\$data{'PQR'} = $data{'PQR'}\n";
print "\$data{'XYZ'} = $data{'XYZ'}\n";
print "\$data{'ABC'} = $data{'ABC'}\n";
print "\$data{'PQR'} = $data{'PQR'}\n";
}
Output:
Recommended Articles
This is a guide to Perl Variables. Here we also discuss the Introduction and how to declare perl variables? along with different examples and its code implementation. You may also have a look at the following articles to learn more –