logo codesdope

References in Perl


In Perl, we can also make a reference to a variable. It means that we can refer to the location of a variable in memory or we can point to the memory of a variable or we can make pointer of a variable. We use backslash (\) operator to create a reference to a variable. Let's see this:

use strict;
use warnings;

my @a = (1,2,3);
my $b = \@a;
print "$b\n";

my %c = ('x'=>1,'y'=>2);
my $d = \%c;
print "$d\n";
Output
ARRAY(0x240b568)
HASH(0x240b580)

We have used $b and $d inside the print function and these are the references of array and hash respectively. So, the address of their memory location got printed on the screen. It is really interesting to print the address of a variable which we are using.

Dereferencing


To access the variable from its reference, we write $, @ and % before the reference of scalar, array and hash respectively. For example, for getting the value of a scalar from its reference, we use $$, for array @$ and %$ for the hash. This is called dereferencing. Let's see an example:

use strict;
use warnings;

my @a = (1,2,3);
my $b = \@a;
print "@$b\n";

my %c = ('x'=>1,'y'=>2);
my $d = \%c;
print %$d,"\n";
Output
1 2 3
x1y2

As you saw that @$b printed the value of the array for which b is the reference and %$d printed the value of hash for which d is the reference.

We can use ref to know the type of a variable. Let's see this in action:

use strict;
use warnings;

my @a = (1,2,3);
my $b = \@a;
print ref($b),"\n";

my %c = ('x'=>1,'y'=>2);
my $d = \%c;
print ref($d),"\n";
Output
ARRAY
HASH

Dynamic reference to array and hash


We can create a dynamic reference of an array by surrounding it with [ ] and hash by surrounding it with { }. The arrays and hashes hold scalar values only and do not directly contain other arrays or complex data structures in Perl, but we can achieve this using this reference. Let's see an example to do this:

use strict;
use warnings;

#reference to array
my $r = [1,2,3];

#array inside array
my $r1 = [1,2,[3,4]];

#reference to hash
my $r2 = {'x'=>1,'y'=>2};

#hash inside hash
my $r3 = {'x'=>1,'y=>2','z'=>[1,2],'w'=>{'a'=>3,'b'=>4}};
Output

-> operator


-> is used on the reference of an array or a hash to access the elements of the array or the hash.
We use $referecnce->[index] to access the elements of an array and reference->{'key'} to access the values of a hash.

Suppose we have $r1 = [1,2,[3,4]], then r1->[0] will give its first element, r2->[1] will give its second element.
r->[2][0] will give you its first element of third element which is an array (or reference to an array). Its third element is [3,4] which is an array and the first element of [3,4] is 3, so r[2][0] will give 3.

Similarly, $r2->{'x'} will give 1 for $r2 = {'x'=>1,'y'=>2}. Let's see an example to use -> operator.

use strict;
use warnings;

#reference to array
my $r = [1,2,3];

#array inside array
my $r1 = [1,2,[3,4]];

#reference to hash
my $r2 = {'x'=>1,'y'=>2};

#hash inside hash
my $r3 = {'x'=>1,'y'=>2,'z'=>[1,2],'w'=>{'a'=>3,'b'=>4}};

print "$r->[0]\n";
print "$r1->[1]\n";
print "$r1->[2][0]\n";
print "$r2->{'x'}\n";
print $r3->{'w'}->{'a'},"\n";
print $r3->{'z'}->[0],"\n";
Output
1
2
3
1
3
1

References to functions


We can also create reference to a function. This is done by using sub without any name of the function. Let's see an example explaining this:

use strict;
use warnings;

my $r = sub{
  print "I am anonymous function\n";
};
Output

Here, we have used a variable 'r' for the reference of the function.

Notice that we have used a semicolon (;) after function ({};).

We use & to call a function by its reference. We use &$reference_name(parameters) to call a function having 'reference_name' as its reference and 'parameters' are the passed arguments to the function. Let's see this in action:

use strict;
use warnings;

my $r = sub{
  print "I am anonymous function\n";
  print "$_[0]\n";
};

&$r(54);
Output
I am anonymous function
54

Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.