Updated June 23, 2023
Definition of Perl Data Types
Perl data types have been divided into scalars, array, and hash data types. Data type in Perl is defined as which type of data is held by perl variable; on the type of data, we have defined data type to the variables. In Perl language, there is no need to define the type of data interpreter will choose it automatically based on the type or context of the data. If we assign integers and strings into two different variables without defining any data type, the Perl interpreter will choose based on the data assigned to the variables.
Perl Data Types with Examples
Below are the data types we used while writing code in Perl language. It was mainly divided into three types as follows –
- Scalars
- Array
- Hash
The scalar data type starts with my package name and precedes with a dollar ($) sign. Scalar data types state that a single unit data Array data type is an ordered list of scalar variables. Array data type starts with @ sign. The hash data type is a set of key or value pairs. Hash data type begins with a % sign.
Below are the description and examples of Perl data types which are as follows.
1. Scalars Data Type
- The scalar data type is considered as a single data; it can be considered an integer or string. We can also consider it as floating point number.
- The scalar data type is preceded with a $ sign and starts with my package name. A scalar data type is a single unit of a data type.
- The scalar data type is divided into three types.
- Integer (Number)
- String
- Floating point numbers.
Below is an example and description of scalar integer, string, and floating number data types.
A. Scalar Integer (number) Data Types
- Scalar integer data type state that declares and initializes an integer data type. But there is no need to define an integer; the interpreter automatically assigns an integer data type to the variable.
The below examples show integer scalar data type.
Example:
my $emp_id = 1;
my $emp_age = 20; # this is scalar integer data type variable declaration.
my $emp_salary = 10000;
use warnings;
if (0<1)
{
print "$emp_id\n";
print "$emp_age\n";
print "$emp_salary\n";
}
Output:
B. Scalar String Data Types
- You enclose scalar string data type variables in single or double quote signs. But a quote is not an actual part of a string. It is just specified as the beginning and ending of the string.
- There are two types that divide scalar string data types as follows.
- Single quoted
- Double quoted
a. Single Quoted
- Below is an example of a single-quoted string data type as follows.
- In below example states that a single quote string data type considers all variables as strings. We have defined tab value in full name, but a single quote will not identify it.
Example:
my $emp_fname = 'AB';
my $emp_lname = 'CD'; # this is scalar string data type variable declaration.
my $emp_address = 'PUNE';
my $emp_fullname = 'AB \t CD';
use warnings;
if (0<1)
{
print "$emp_fname\n";
print "$emp_lname\n";
print "$emp_address\n";
print "$emp_fullname\n"
}
Output:
b. Double Quoted
- Below is an example of double-quoted string data type as follows.
- The example below states that the double quote string data type does not consider all variables as strings. In full name, we have to define tab value; a single quote will not identify it. But a double quote string will identify the tab value.
Example:
my $emp_fname = "AB";
my $emp_lname = "CD"; # this is scalar string data type variable declaration.
my $emp_address = "PUNE";
my $emp_fullname = "AB \t CD";
use warnings;
if (0<1)
{
print "$emp_fname\n";
print "$emp_lname\n";
print "$emp_address\n";
print "$emp_fullname\n"
}
Output:
C. Scalar Floating Point Number Data Types
- The below example shows integer scalar data type.
- The scalar floating point number data type defines floating point numbers.
Example:
my $emp_salary = 100.50;
my $emp_age = 25.6; # this is scalar floating point number data type variable declaration.
use warnings;
if (0<1)
{
print "$emp_salary\n";
print "$emp_age\n";
}
Output:
2. Arrays Data Type
- The array data type is an ordered list of scalar data types. Array data type starts with @ sign.
- We need to use a $ sign to display a single element of array variables to initialize an array of variables.
- Below is an example of an array data type.
Example:
@emp_ages = (20, 25, 30); # integer type variable array declaration.
@emp_names = ('ABC', 'PQR', 'XYZ'); # String type variable array declaration.
@emp_salary = (10.1, 10.2, 10.3); # Floating point number array declaration.
use warnings;
if (0<1)
{
print "\$emp_ages[0] = $emp_ages[0]\n";
print "\$emp_ages[1] = $emp_ages[1]\n";
print "\$emp_ages[2] = $emp_ages[2]\n";
print "\$emp_names[0] = $emp_names[0]\n";
print "\$emp_names[1] = $emp_names[1]\n";
print "\$emp_names[2] = $emp_names[2]\n";
print "\$emp_salary[0] = $emp_salary[0]\n";
print "\$emp_salary[1] = $emp_salary[1]\n";
print "\$emp_salary[2] = $emp_salary[2]\n";
}
Output:
3. Hash Data Type
- Hash data type in Perl is a set of key or value pairs. Hash variables start with a % sign.
- Initialization of hash variables, we need to use a % sign to display a single element of hash variables.
- Below is an example of a hash data type.
Example:
%data = ('AB', 20, 'PQ', 25, 'XY', 30, 'AB', 35, 'PQ', 40);
# Hash data type variable declaration.
use warnings;
if (0<1)
{
print "\$data{'AB'} = $data{'AB'}\n";
print "\$data{'PQ'} = $data{'PQ'}\n";
print "\$data{'XY'} = $data{'XY'}\n";
print "\$data{'AB'} = $data{'AB'}\n";
print "\$data{'PQ'} = $data{'PQ'}\n";
}
Output:
Conclusion
Perl divides its data types into scalars, arrays, and hash data types. The data type in Perl is defined by the type of data held by a Perl variable; on the type of data, we have limited data types for the variables. The data type is essential.
Recommended Articles
We hope that this EDUCBA information on “Perl Data Types” was beneficial to you. You can view EDUCBA’s recommended articles for more information.