References in Perl are actually quite simple.
The perlref man page makes it seem a lot more complicated than it is,
partly because it goes into a lot of detail and partly because there are so many different ways to do everything in Perl.
Please follow along, if you can, by typing these snippets of Perl code into a test program and running them as you go through this. It will help you to understand the concepts as we build on one piece of knowledge to get to the next.
Create a reference to an object in Perl by using the reference operator \:
$prez = 'George';
$refprez = $prez;
Now $refprez is a reference to $prez.
You can now dereference $refprez to get at the data in $prez:
print "prez is $$refprez\n"; # prints the contents of prez
$$refprez = 'Bill'; # assigns a new value to prez
In effect, the reference is really just a scalar that contains the address of another variable, and the type of variable to which it points.
In fact, if you print a reference with print you will see something like this:
SCALAR(0x80b8988)
Perl has a special function called ref that returns the type of reference, or 0 if it's not a reference.
$reftype = ref $refprez; # assigns SCALAR to $reftype
You can also use ref to test a variable, to see if it's a reference:
# call foo if $refprez is a reference
foo($refprez) if ref $refprez;
Perl Reference Quiz
Click the Quiz link below to take a short multiple-choice quiz on what you've so far learned about Perl references. Perl Reference - Quiz