# Create References in Perl

PERLServer Side ProgrammingProgramming Scripts

A Perl reference is a scalar data type that holds the location of another value which could be scalar, arrays, or hashes. Because of its scalar nature, a reference can be used anywhere, a scalar can be used.

It is easy to create a reference for any variable, subroutine or value by prefixing it with a backslash as follows −

$scalarref = \$foo;
$arrayref = \@ARGV;$hashref = \%ENV;
$coderef = \&handler;$globref = \*foo;

You cannot create a reference on an I/O handle (filehandle or dirhandle) using the backslash operator but a reference to an anonymous array can be created using the square brackets as follows −

$arrayref = [1, 2, ['a', 'b', 'c']]; Similar way you can create a reference to an anonymous hash using the curly brackets as follows − $hashref = {
'Clyde' => 'Bonnie',
};

A reference to an anonymous subroutine can be created by using sub without a subname as follows −

\$coderef = sub { print "Boink!\n" };
Published on 29-Nov-2019 07:13:32