What are references in Perl

In Perl, references are special variables that hold the location or address of another variable. They are useful for creating complex data structures such as arrays of arrays or hashes of hashes, and they allow for more flexible and powerful programming techniques.

References can be created for scalars, arrays, hashes, or even subroutines. They are typically created by using a backslash (&) operator before the variable that you want the reference to point to.

Here's an example of how to use references in Perl:

# Create a scalar reference my $scalar = 5; my $scalar_ref = \$scalar; # Create an array reference my @array = (1, 2, 3); my $array_ref = \@array; # Create a hash reference my %hash = (key1 => 'value1', key2 => 'value2'); my $hash_ref = \%hash; # Dereferencing examples print $$scalar_ref; # prints 5 print @{$array_ref}; # prints 123 print $hash_ref->{key1}; # prints value1

references Perl data structures programming techniques