References and Data Structures


To use the data that is referred to by a reference, you have to explicitly dereference the reference.

For example, in the previous slide, the reference $ref_to_synonyms pointed to a list of synonymous names of the PLD1 protein.

To use this list, we have to dereference $ref_to_synonyms. The syntax is: @{$ref_to_synonyms}. This will give us the array (the list of synonyms) which is pointed to by $ref_to_synonyms.

Let's dereference each of the references from the previous slide, and store the data we thus received in another variable:

$a = ${$ref_to_name};
@b = @{$ref_to_synonyms};
%c = %{$ref_to_information};

Note: the curly brackets here are optional.

To summarize all the syntactic forms:

Original variable Referencing Dereferencing
$protein_name $ref_to_name = \$protein_name $a = ${$ref_to_name};
@synonyms $ref_to_synonyms = \@synonyms; @b = @{$ref_to_synonyms};
%information $ref_to_information = \%information; %c = %{$ref_to_information};

After dereferencing, $a, @b and %c contain exactly the same data as in $protein_name, @synonyms and %information (respectively) from the previous slide.

However, note that $a, @b and %c do not "know" or "remember" anything about $protein_name, @synonyms and %information.

You may wonder: why bother referencing and dereferencing a variable if we eventually get the same data? In the next slide we will see an example that uses this mechanism for passing variables to a subroutine. Later we will learn that the use of references is essential for creating complex data structures.

Table of Contents.
Previous | Next.