introduction to Perl 5 for web developers Anonymous References Another way to create a reference involves creating a reference to an anonymous array, hash or subroutine. A reference to an anonymous array, hash or subroutine is a reference to a data structure that has no name of its own.

Why would you want to do this?

Well, perhaps you need the array only for a short moment in your program and do not want to clutter your name space on such an ephemeral object. With an anonymous reference, there is no name. The data gets read and written in Perl's memory space utilizing only the reference to the data structure mapped to an anonymous location in memory.

Whichever the case, to create a reference to an anonymous array, you use the square brackets ([]) such as in the following example in which we create a matrix of an anonymous array inside an anonymous array:

     my $arrayref = ['a', 'b', 'c',
                    ['1', '2','3']];

Notice that now you can get the value of "3" by using the following:

     print $arrayref->[3][2];

Similarly, you can create and access an anonymous hash using something like:

     my $hash_reference = {
     	'name'  => 'Gunther B',
        'phone' => '123-345-6789'
     print $hash_reference->{'name'};
Previous | Next | Table of Contents