Perl References

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

A reference is a scalar value that points to a memory location that holds some type of data. Everything in your Perl program is stored inside your computer's memory. Therefore, all of your variables and functions are located at some memory location. References are used to hold the memory addresses. When a reference is dereferenced, you retrieve the information referred to by the reference. There are Six types of references. A reference can point to a scalar, an array, a hash, a glob a function, or another reference. The table below shows how the different types are valued with the assignment operator and how to dereference them using curly braces.

The Six Types of References Reference Assignment

$refScalar = \$scalar; $refArray = \@array; $refHash = \%hash; $refglob = \*file;

$ref Function = \&function $ref Ref = \$ref Scalar;

Eg.: Reference to a scalar value $x = 23;

# create a reference $c = \$X;

# dereferencing $z = $$c;

print "$c\n"; will display: 23

How to Dereference $($refScalar} is a scalar value.
@{$refArray) is an array value.
%($refHash) is a hash value.

&($refFunction} is a function location. $(${$refScalar} is a scalar value.
Eg.: Reference to an array
@a (1,2,3);
$x \@a; @copy = @($x);
Eg.: Reference to a hash
%z ('El "=>"Ram"," E2"=> "Gum");
$c \%z; %copy = %(c);
Eg.: Reference to a sub routine
Here is a refernce to a sub routine named called
$a = \&called;
to call the function through the refernce (with "test" as argument): &($a)(" test");
Eg.: Reference to a reference:
$x 1001$rx \$x; # refernce to $x is in $rx
$rrx = \$rx;
# refernce to reference $rx is in $rrx $val = $$$rrx;
# access the value of $x
print "$val\n";
will display: 100

Essentially, all you need to do in order to create a reference is to add the backslash to the front of a variable.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.