─ţŕˇýňݲÓ÷Ŕ 
HTML CSS PHP PERL ń­ˇŃţň

Reference Basics

 
Previous Table of Contents Next

Reference Basics

You create and assign a normal scalar variable by using an assignment operator, as follows:


$a="Stones";   # A normal scalar


After this snippet, a scalar variable called $a is created, and it contains the string "Stones". Now, somewhere within the computer's memory, there is a place labeled $a that contains that string, as illustrated here:

If you were to assign the scalar $a to $bŚlike $b=$a;Śyou would wind up with two copies of the data, withtwo different names, as shown here:

Having two copies might be acceptable if you want separate, independent copies of the data. However, if you want both $a and $b to refer to the same piece of dataŚnot a copyŚ, you must create a reference. Just as the library catalog card for a book does not contain a copy of the book's text, a reference does not contain any real data; it is simply a pointer to a piece of data. The reference is usually stored in another scalar variable.

To create a reference to any given variable, you must put a backslash in front of the variable name with its type identifier. For example, to create a reference to $a called $ref, you would assign the reference to $ref as follows:


$ref=\$a;   # Create a reference to $a


This assignment creates a situation like the following:

$ref doesn't contain any data for itself; it simply is a reference to $a. The variable $a isn't changed; it can still be assigned to ($a="Foo") or displayed (print $a) as normal.

The variable $ref now contains a reference to $a. You cannot simply manipulate $ref as you would $a, because it doesn't contain a normal scalar value. In fact, printing $ref would display something similar to SCALAR(0x0000). To get to the value inside $a through $ref, you must dereference $ref. Think of dereferencing as following the arrow in the preceding block diagram. To print the value of $a tHRough the reference $ref, you would need to use an extra $ like this:


print $$ref;


In the preceding snippet, $ref contains, of course, the reference. The extra $ tells Perl that the reference in $ref refers to a scalar value. The scalar value that $ref refers to is fetched and printed.

You can also modify the original value through the referenceŚsomething you can't do with a copy. The following code modifies the original value in $a:


$$ref="Sticks";    # De-references $$ref


This modification creates something like the following:

If you had used $ref instead of $$ref


$ref="Break";


then the reference stored in $ref would have been destroyed and replaced with a real value, as shown here:

After the preceding snippet, $ref no longer contains a reference; it is just another scalar. You can assign such a reference as you would any other scalar value:


$name="Gandalf";

$nref=\$name;        # Has a reference to $name

$oref=$nref;         # Has a copy of the reference to $name


You get this result

After the preceding snippet, $$oref and $$nref both can be used to get to the value "Gandalf". You can also store a reference to a reference,as follows:


$book="Lord of the Rings";

$bref=\$book;    # A reference to $book

$bref2=\$bref;   # A reference to $bref (not to $book!)


In this case, the chain of references looks like the following:

To print the book title given $bref, you would use $$bref. To print the book title given $bref2, you would use $$$bref2, with an extra dollar sign, requiring one more level of dereferencing to get to the original value.

References to Arrays

References can also be created to arrays and hashes. Such references are created in the same way a reference is created to a scalarŚby using a backslash:


$aref=\@arr;


Now the scalar variable $aref contains a reference to the entire array @arr. Visually, it might resemble the following:

To access portions of @arr using the reference $aref, you would use one of the following examples:

$$aref[0]

The first element of @arr

@$aref[2,3]

A slice of @arr

@$aref

The whole of array @arr


For clarity, you can use braces to separate the reference from the portions dealing with the array itself, as shown here:

$$aref[0]

is the same as

${$aref}[0]

@$aref[2,3]

is the same as

@{$aref}[2,3]

@$aref

is the same as

@{$aref}


For example, to print all the elements of @arr using the array reference $aref, you can use this code:


foreach $element (@{$aref}) {

    print $element;

}


References to Hashes

To create a refer
Previous Table of Contents Next

© 2000- NIV