====== Reference ======
Reference were quite complex, so we made a resume from all info we found and try the make something clear and short. But they are extremly powerfull and is a must have if you do programming in perl. Of course we probably miss something so feel free to tell us what you think.
=== Rule#1 : Add "\" before your variable and get the ref ===
$array_ref = \@array; # $aref is a reference to @array
$hash_ref = \%hash; # $href is a reference to %hash
$scalar_ref = \$scalar; # $sref is a reference to $scalar
$xy = $array_ref; # $xy is a copy of a reference to @array => it is a reference to @array
$array[2] = $hash_ref; # $array[3] is a copy of a reference to %hash => it is a reference to %hash
$scalar = $array[2] # $scalar is a copy of a copy of a reference to %hash => it is a reference to %hash
$array_ref = [ 1, "foo", undef, 13 ]; # $array_ref is a reference to the array (1, "foo", undef, 13)
$hash_ref = { APR => 4, AUG => 8 }; # $hash_ref is a reference to the hash (APR => 4, AUG => 8) or (APR, 4, AUG, 8)
Note: the bracket are the same as the one that are used to access an element!
@array = (1, 2, 3); $array_ref = \@array; can be written $array_ref = [ 1, 2, 3 ];
$hash = (APR, 4, AUG, 8); $hash_ref = \%hash; can be written $hash_ref = { APR => 4, AUG => 8 }
In the second (short) form, we do not have an array variable or a hash variable explicitly: we call the "anonymous array" and "anonymous hash"
=== Rule#2: Code with non-ref variable "var" and replace them (dont remove the $@%) with your ref variable "{$var_ref}" : you get your variable dereferenced and you can access the real value. ===
Reverse rule or dereferencing: (Alternative: Dereferencing with arrow: recommended)
@a = @{$array_ref}; A copy of the array which is referenced (can be a bug structure so be carefull)
%h = %{$hash_ref}; A copy of the hash which is referenced (can be a bug structure so be carefull)
$s = ${$scalar_ref}; A copy of the scalar which is referenced (not that big...)
and now for the elements of array or hash:
with:$arref = \@array; $href = \%hash; $scaref = \$scalar
$array[3]=2; $ array [3]=2
is equivalent to ${$arref}[3]=2; ${$arref}[3]=2
$hash{3}=2; $ hash {3}=2
is equivalent to ${$href}{3}=2; ${$ href}{3}=2
@ array =
@{$arref} =
% hash =
%{$ href} =
$ scala =
${$scref} =
{$ }
This means we can code without ref and change your variable at the end var to {$var_ref} in any cases !!!
Note2: a reference is also a scalar, but be carefull:
with:$array_ref = \@array; $hash_ref = \%hash; $scalar_ref = \$scalar
$a = ${$array_ref}; $a is an array in a "scalar context", so equivalent to "scalar @array" or $(@array) or $a = @array; $a is nb of element of the array!
$h = ${$hash_ref}; $h is an hash in a "scalar context", so equivalent to "scalar $hash " or $(%hash ) or $h = %hash; $h a storage size of the hash (avoid)
$s = ${$scalar_ref}; $s is a copy of the $scalar, so equivalent to "$s = $scalar... so ${\$scalar} is equivalent to $scalar!
Note1: If we deref a ref we obtain the variable that can be of all type!: logic
Note2: if we ref a deref we obtain the ref, logic!
Note3: Why would you do that?...
=== Trivial case and simplification, even it works... ===
Trivial case and simplification, even it works...
${\$scalar} => $scalar
@{\@array} => @array
%{\%hash} => %array
\${$scalar_ref} => $scalar_ref
\@{$array_ref} => $array_ref
\%{$hash_ref} => $array_ref
So you shoud never have this:
${\$ safe to remove if the variable is a scalar
@{\@ safe to remove if the variable is an array
%{\% safe to remove if the variable is a hash
\${$ safe to remove if the variable is a scalar ref
\@{$ safe to remove if the variable is an array ref
\%{$ safe to remove if the variable is a hash ref
Note1: dont forget to remove the ending bracket!
but what about that :
\${$array_ref}[1] the referencing is apply a the end so
1. ${$array_ref}[1] we have an element of the array
2. \${$array_ref}[1] we have a scalar ref of the element of the array
As you can see it is not safe to be removed as the variable is not a scalar....