This node exists to demonstrate how to create references to various data types before actually storing any data. This node has been created because I am tired of trying to figure it out from scratch every time.

At this time, I am only aware of references to hashs and to arrays. Since objects are just hashes bound to some special syntax, I will ignore them.

Creating a reference to an empty hash

my ($reference);
$reference = {};

Creating a reference to an empty array

my ($reference);
@{$reference} = ();

You can also create references to subroutines, among other things.

Creating a reference to a predefined subroutine

# define the subroutine
sub foo {
    print "Hello from foo()\n";
}

# the \ prefix means "address of" to create a reference
my $reference = \&foo;

# the & de-references the $reference variable, and the subroutine is executed
&$reference();

Log in or register to write something here or to contact authors.