|
Who
is this for This is for those who want to have a brief
overview of references.
What you should know Basic
Perl programming
Introduction In most
high-level languages (ie. Visual Basic, COBOL - yes, this still
exists), most variables are accessed through the variable name. In
low-level languages (ie, C, C++, Assembler), you can access a
variable indirectly, either through an address or reference. Perl
allows you to do this through references.
References are values in a
variable that references - or points - to a symbol. There are two
types of references in Perl: symbolic and hard. This
article is about hard references. We will not be discussing symbolic
references because these are not used much.
Creating References References
are stored in scalar variable. You can refer to any type of variable,
subroutine or value.
You create references by
specifying a reverse backslash \ before the symbol being
referenced:
Example 1
|
#
Assign reference of $scalar to $ref $ref =
\$scalar;
# Assign
reference of @array to $refArr $refArr =
\@array;
# Assign
reference of # element 0 of array @anotherarray #
to $array[1] $array[1] = \$anotherarray[0];
# Assign
reference of %hash to $refHash $hash{'sample'} =
'This is a sample value'; $refHash = \%hash;
|
Since the array or hash
elements are scalar, you can store references in them. You can in
effect create an array of references or hash of references.
How does it work Once
you have referenced the symbol, you have to use it. Since the
reference is stored to a scalar variable, the reference is treated as
a scalar.
Example
2 You have two scripts:
|
Line
|
Script 1
|
Script 2
|
|
1
|
$scalar = 1;
|
$scalar = 1;
|
|
2
|
$ref = $scalar;
|
$ref = \$scalar;
|
|
3
|
$ref++;
|
$ref++;
|
Notice that the two scripts are
similar except for line 2. Line 2 of Script 1 assigns the value of
the $scalar variable to $ref. Line 2 of Script 2
assigns the reference of the $scalar variable to $ref.
On execution of Script 1, the
result is 2. On execution of Script 2, the result is not 2 because
you are adding 1 to the reference instead of the value 1.
Dereferencing To use the
value of the referenced symbol, you have to dereference it. To do
this, you use the $ sign. The $ sign is preceeded by
the symbol of the variable.
In
Example 1
|
Dereference
|
Do This
|
Explanation
|
|
$scalar
|
$$ref
|
The first $
signifies that the dereferenced variable is a scalar.
|
|
@array
|
@$refArr
|
The @ means
that the resulting variable is an array.
|
|
@array[3]
|
$$refArr[3] or
the 'cleaner' way: $refArr->[3]
|
The first $
means that the resulting variable $array[3]is a scalar -
even if the variable itself (@array) is an array. The ->
means that $refArr is a reference.
|
|
%hash
|
%$refHash
|
The % means
that the resulting variable is a hash.
|
|
$hash{'sample'}
|
$$refHash{'sample'}
or the 'cleaner' way: $refHash->{'sample'}
|
The first $
means that the resulting variable is a scalar. The ->
sign means that $refHash is a reference.
|
What is being referenced There
will be times when you want to know what type of symbol is being
referenced. You can find out by using the ref function.
Again, using Example 1
above: ref($ref) produces SCALAR(0x9999999) where
9999999 is any valid hexadecimal number that Perl uses to
refer to the variable.
ref($refArr) produces
ARRAY(0x9999999)
Why will you use
this References are used specifically when you create packages
or objects, to be exact. You can also use this to create
multi-dimensional arrays.
This will be discussed further
when we get to object-oriented programming.
|