Perl is a dynamically-typed language, which means that data types are determined at runtime. Perl supports a wide range of data types that are grouped into several categories including scalar, array, hash, and reference.
1. Scalar: A scalar is a single value that can be a number, a string, or a reference. Scalar variables start with a dollar sign ($) and can be declared using the my function. Some examples of scalar variables are:
my $x = 10; # integer scalar
my $name = "John"; # string scalar
my $ref = @array; # reference scalar
2. Array: An array is an ordered list of scalar variables. Array variables start with the at sign (@) and can be declared using the my function. Some examples of array variables are:
my @numbers = (1, 2, 3, 4); # integer array
my @names = qw(John Mary Jane); # string array
my @ref_array = ($x, $y, $z); # array of reference scalars
3. Hash: A hash is an unordered list of key-value pairs where each key is unique. Hash variables start with a percent sign (%) and can be declared using the my function. Some examples of hash variables are:
my %grades = ('John' => 90, 'Mary' => 85, 'Jane' => 95); # hash of integer values
my %age = ('John' => 30, 'Mary' => 25, 'Jane' => 20); # hash of integer values
my %ref_hash = ('x' => $x, 'y' => $y, 'z' => $z); # hash of reference scalars
4. Reference: A reference is a scalar variable that points to another variable, such as a scalar, an array, or a hash. Reference variables start with the backslash (
) and can be declared using the my function. Some examples of reference variables are:
my $scalar_ref = $x; # reference scalar to $x
my $array_ref = @numbers; # reference to @numbers
my $hash_ref = %grades; # reference to %grades
In addition to these basic data types, Perl also provides support for complex data types such as regular expressions, file handles, and objects.