(Perl:)
A "real" reference refers to "slot" in Perl's memory holding a scalar, array or hash. A symbolic reference is almost nothing like that; instead, it is a reference to a global variable by a name that is a string. You cannot refer to a my variable using a symbolic reference: names of my variables are lost after compile time, while symbolic reference lookup occurs at run time!

The syntax used is similar (@{CODE EVALUATING NAME OF ARRAY} is an array reference), hence the similar names.

Symbolic references answer the age-old question "how do I access the contents of a variable stored in a variable?"; however, they're almost always NOT what you want to do. Using a hash is almost always a better idea than polluting your global namespace. For this reason, use strict prohibits the use of symbolic references.

Symbolic references are useful for performing various symbol table hacks, usually in conjunction with the symbol's typeglob. We can write a routine to put a constant into our namespace:

sub mk_const {
  my ($var, $val) = @_;
  no strict 'refs';
  &{$var} = sub() { $val }
}

# Useful mathematical constant
mk_const PI => 4.0;

sub area {
  my $r = shift;
  PI*$r*$r
}

Since a symbolic reference must refer to a variable, you cannot have a symbolic reference to an element of an array or a hash -- they have no symbol naming them. Saying ${'a[3]'} = 17 doesn't make element 3 of @a equal 17; it creates an oddly-named scalar (which has square brackets in its name) and assigns 17 to that.

Log in or register to write something here or to contact authors.