Perl References
A reference is a pointer; a Perl reference is a scalar type that can point to variables, arrays, hashes (also known as associative arrays), or even subroutines, and can be used anywhere in a program.
Creating References
When defining a variable, adding a backslash () in front of the variable name creates a reference to that variable, for example:
$scalarref = \$foo; # Reference to a scalar variable
$arrayref = \@ARGV; # Reference to an array
$hashref = \%ENV; # Reference to a hash
$coderef = \&handler; # Reference to a subroutine
$globref = \*foo; # Reference to a GLOB handle
In arrays, we can use anonymous array references, defined with square brackets ([]):
$aref = [ 1, "foo", undef, 13 ];
Elements of an anonymous array can still be anonymous arrays, so we can construct arrays of arrays, and create arrays of any dimensionality.
my $aref = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
In hashes, we can use anonymous hash references, defined with curly braces ({}):
$href = { APR => 4, AUG => 8 };
We can also create an anonymous subroutine reference without a subroutine name:
$coderef = sub { print "tutorialpro!\n" };
Dereferencing
Dereferencing can be done using $, @, or % depending on the type, as shown in the following example:
Example
#!/usr/bin/perl
$var = 10;
# $r references $var scalar
$r = \$var;
# Output the value of the variable stored locally in $r
print "$var is : ", $$r, "\n";
@var = (1, 2, 3);
# $r references @var array
$r = \@var;
# Output the value of the variable stored locally in $r
print "@var is: ", @$r, "\n";
%var = ('key1' => 10, 'key2' => 20);
# $r references %var hash
$r = \%var;
# Output the value of the variable stored locally in $r
print "%var is : ", %$r, "\n";
Executing the above example results in:
10 is : 10
1 2 3 is: 123
%var is : key110key220
If you are unsure of the variable type, you can use ref to determine it. It returns the following values, or false if none are found:
SCALAR
ARRAY
HASH
CODE
GLOB
REF
Example:
#!/usr/bin/perl
$var = 10;
$r = \$var;
print "Reference type of r : ", ref($r), "\n";
@var = (1, 2, 3);
$r = \@var;
print "Reference type of r : ", ref($r), "\n";
%var = ('key1' => 10, 'key2' => 20);
$r = \%var;
print "Reference type of r : ", ref($r), "\n";
Executing the above example results in:
Reference type of r : SCALAR
Reference type of r : ARRAY
Reference type of r : HASH
Circular References
Circular references occur when two references contain each other. You need to use them carefully, as they can lead to memory leaks, as shown in the following example:
Example
#!/usr/bin/perl
my $foo = 100;
$foo = \$foo;
print "Value of foo is : ", $$foo, "\n";
Executing the above example results in:
Value of foo is : REF(0x9aae38)
Function References
Function reference format: \&
Calling a referenced function format: & + the created reference name.
Example:
#!/usr/bin/perl
# Function definition
sub PrintHash {
my (%hash) = @_;
foreach $item (%hash) {
print "Item : $item\n";
}
}
%hash = ('name' => 'tutorialpro', 'age' => 3);
# Create a reference to the function
$cref = \&PrintHash;
# Call the function using the reference
&$cref(%hash);
Executing the above example results in:
Item : age
Item : 3
Item : name
Item : tutorialpro