Definition of Perl Data Types
Perl data types have basically divided into scalars, array and hash data types. Data type in perl is defined as which type of data 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 assigning integer and string into two different variables without defining any data type the perl interpreter will choose on the basis of data assigned to the variables.
Perl Data Types with Examples
Below are the data types which we have used while writing code in perl language. It was mainly divided into three types are as follows.
- Scalars
- Array
- Hash
Scalar data type starts with my package name and precedes with a dollar ($) sign. Scalar data types states that a single unit data Array data type is nothing but a ordered list of scalar variables. Array data type starts with @ sign. Hash data type is nothing but a set of key or value pairs. Hash data type starts with % sign.
Below are the description and examples of Perl data type which are as follows.
1. Scalars Data Type
- Scalar data type is considered as a single data, it can be considered as either integer or string we can also consider it as floating point number.
- Scalar data type is preceded with a $ sign and start with my package name. Scalar data type is single unit of a data type.
- Scalar data type is basically divided into three types.
- Integer (Number)
- String
- Floating point numbers.
Below is example and description of scalar integer, string and floating number data types.
Scalar Integer (number) Data Types
- Scalar integer data type state that declare and initialize an integer data type. But there is no need to define integer, interpreter automatically assign integer data type to the variable.
The below examples shows that 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:
Scalar String Data Types
- Scalar string data types variable are covered in a single or double quote sign. But quote is not an actual part of a string it is just specify that the beginning and ending of the string.
- Scalar string data types is basically divided into two types are as follows.
- Single quoted
- Double quoted
Single Quoted
- Below is the example of single quoted string data type are as follows.
- In below example states that single quote string data type is consider all variables as string. In full name we have define tab value but single quote it 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:
Double Quoted
- Below is the example of double quoted string data type are as follows.
- In below example states that double quote string data type is not consider all variables as string. In full name we have define tab value, single quote it will not identify it. But 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:
Scalar Floating Point Number Data Types
- Below example shows integer scalar data type.
- Scalar floating point number data type is used to define floating point number.
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
- Array data type is nothing but ordered list of scalar data type. Array data type starts with @ sign.
- Initialization of an array variables we need to use a $ sign to display single element of array variables.
- Below is the example of 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 nothing but set of key or value pairs. Hash variables starts with % sign.
- Initialization of a hash variables we need to use a % sign to display single element of hash variables.
- Below is the example of 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 data types is basically divided into three type’s scalars, array and hash data types. Data type in perl defined as which type of data hold by perl variable, on the type of data we have defined data type to the variables. Data type is very important.
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.
600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6
View Course
Related Courses