Reference Variables and Type Globbing


Beginning | Previous | Next

Hard References

Hard references refer to an actual value or some other chunk of data. You may think of them in the C++ sense, in that, they can only exist if they refer to another variable. Some examples are: $scalar_reference = \$value; $const_reference = \3.1415; $array_reference = \@list_of_some_kind; $hash_reference = \%table; $code_reference = \&interrupt_handler; $glob_reference = \*DATA_FILE; The backslash operator "\" is similar to the address-of operator (&) in C/C++.

Using Hard References

To access hard references, you treat $reference_name as the variable name and use the appropriate variable symbol (ie, $, @, % or &) to dereference it, as in: print "scalar value = $$scalar_reference\n"; print "array values = @$array_reference\n"; print "hash keys and values = %$hash_reference\n"; $a = $$array_reference[3]; # access 4th element # yet another way to access the 4th element $a = ${array_reference}[3];

Symbolic References

If a variable is not a reference to another variable and you dereference it, then you have a symbolic reference, such as: $sym = 'width'; $$sym = 100; # sets $width to 100 $$sym[3] = 20; # sets $width[3] to 20 $sym->[3] = 20; # same &$sym(); # call &width()

Restricting Symbolic Reference Use

To restrict the use of symbolic references, you can do: use strict 'refs'; and then only hard references will be allowed for the rest of the block. To reset (allow) the use of symbolic references afterward, do: no strict 'refs';

Type Globbing

Typeglobs, represented by an asterisk (*), is a a way to represent all types. With the introduction of references, typeglobs are now only used to reference file handles.

Beginning | Previous | Next
Last Modified: $Date: 1997/09/18 08:38:04 $