References and Data Structures

Creating anonymous references

Until now, we learned how to create a reference to data that already has a variable name. For this, we used the backslash operator.

However, there might be cases when we would like to create a reference to data that has not been stored under any variable name before. For example, we would like to literally define a reference to a list of elements, or to define a reference to data that is organized as a hash. Such data is called anonymous data, and the reference to it is called an anonymous reference.

For example, instead of going through the process:

@synonyms     = ('Phospholipase D1',
                 'Phosphatidylcholine-hydrolyzing phospholipase D1',
                 'Choline phosphatase 1',
                 'EC 3.1.4.4');

$ref_to_synonyms    = \@synonyms;
We can define $ref_to_synonyms directly, as an anonymous array reference:
$ref_to_synonyms = ['Phospholipase D1',
                    'Phosphatidylcholine-hydrolyzing phospholipase D1',
                    'Choline phosphatase 1',
                    'EC 3.1.4.4'];
Pay attention to the syntax: we surround the elements of the referenced array by square brackes, not by parentheses.

To directly define the $ref_to_information hash-reference, we write:

$ref_to_information  =
   {'Catalytic activity'   => 'PC + H(2)O = Choline + PA',
    'Function'             => 'Signal transduction, membrane trafficking',
    'Regulation'           => 'Activated by PIP2, PIP3, ARF1',
    'Subcellular location' => 'Cytoplasmic, and inner membrane-associated',
    'Similarity'           => 'Belongs to the Phospholipase D family'};
To summarize, let's compare the syntax of defining array/hash variables to defining array/hash anonymous references:

Array data
Defining an array variable
@a  = ('x', 'y', 'z');
Defining an array reference
$ref_to_a = ['x', 'y', 'z'];

Hash data
Defining a hash variable
%h  = ('x'=>'k', 'y'=>'l', 'z'=>'m');
Defining a hash reference
$ref_to_h = {'x'=>'k', 'y'=>'l', 'z'=>'m'};

Dereferencing an anonymous reference is the same as dereferencing a reference that points to named data.


Table of Contents.
Previous | Next.