What are the Perl data types?

Perl has three types of data types

  • Scalars : It stores Single unit of items. Variables of these type are declared with $ prefix. These can be Number or string data.
  • Arrays: It is used to store the same type of scalar type
  • Hashes

Scalars in Perl

Scalars types are used to store single unit of data.

Data can be numbers, floating values, String and characters.

$id = 11;             # An integer assignment
$name = "Eric";   # A string
$marks = 45.50;     # A floating point

print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";

Array type in in Perl

Arrays is collection of Scalar items with insertion order.

Arrays in perl declared with variable name prefixed with @

Here is an example

@numbers = (20, 40, 10);
@words = ("one", "two", "three");

Hashs data type in Perl

Hashes also called associate arrays in perl.

It contains key and value pair.

Hashes variable declared with % symbol.

Data can be retrieving using $hashes

\# Perl example on Hashes

%countries = ('India', 'IND', 'United States Of America', 'USA', 'Canada', "CAN");