Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Using Hard References

This section describes how to use hard references - hard references are used in the same way as soft references by replacing identifier with a hard reference expression in curly brackets {}, or using the dereference operator ->.

Similar to soft (symbolic) references, there several syntax formats to use hard references:

1. Directly placing a scalar variable that contains the hard reference in the place where the identifier should be. For example, $ref = \$foo; $$ref = 10; print "$foo";

2. Placing an expression that returns a hard reference in curly brackets {} to replace the identifier. For example, $ref = \$foo; ${$ref} = 20; print "$foo";

3. If a hard reference is used to access a single element of an array, to access a single element of hash, or to call a function, you can remove the name space prefix character $, or &, and replace the identifier with an expression that returns the hard reference, then followed by the dereference operator: ->. For example, $ref = \@foo; $ref->[0] = 30; print "$foo[0]";

4. Of course, hard references can be nested. They can also be mixed with soft (symbolic) references. For example, $a = \$b; $b = \$c; $c = 40; print "$$$a"; Another example is: $a = \$b; $b = 'c'; $c = 50; print "$$$a";

Sections in This Chapter

\* - Creating Hard References

Using Hard References

$$name - Replacing Identifiers by Scalar Variables

${EXPR} - Replacing Identifiers by Expressions

EXPR->* - The Dereference Operator

$$$name - Nested Hard References

\$b-\$a - Using Hard References in Other Operations

Dr. Herong Yang, updated in 2008
Using Hard References