What is references in Perl?

In Perl, a reference is a scalar value that holds the location of another value, which can be a variable, an array, a hash, or even a subroutine. References allow you to create complex data structures like arrays of arrays or hashes of arrays. This can be useful for managing and manipulating data efficiently. By using references, you can also pass large data structures to functions without needing to copy them, saving memory and processing time.

Here’s a brief overview of how to create and use references in Perl:

# Create an array reference my $array_ref = [1, 2, 3, 4, 5]; # Access elements from the array reference print $array_ref->[0]; # Outputs: 1 # Create a hash reference my $hash_ref = { name => 'John', age => 30 }; # Access values from the hash reference print $hash_ref->{name}; # Outputs: John

Perl references Perl data structures Perl arrays Perl hashes