How do I use symbolic references in Perl?

Several points:

  • You aren’t talking about typeglobs, you are talking about symbolic references.
  • Don’t use symbolic references — they lead to hard to track down bugs.
  • In almost any case where symbolic references seem like a good idea, using a data structure based on a hash is the best approach.
  • Consider using Hash::Util to lock your hashes when you don’t want to alter them.
  • Symbolic references don’t work on lexical variables.
  • Typeglobs don’t work on lexical variables.
  • Use lexical variables.
  • Don’t use symbolic references.

See perlreftut for more on references (symbolic and otherwise).
See perldsc for help using data structures.
See perlmod for more on typeglobs.
See perlsub for more on lexical variables.

Here’s an example of using locked hashes to control access to data based on the content of a variable:

use strict;
use warnings;
use Hash::Util qw( lock_hash unlock_hash );

my %data;
lock_hash( %data );
#Altering %data is a fatal exception.

unlock_hash( %data );

%data = (
    'bob' => [ 'jerk', 'genius' ],
);
lock_hash( %data );


for my $name (qw/ bob  margaret /) {
    my $info = $data{$name}; # Fatal error when accessing data for margaret.
    print "$name is a\n";
    print map "\t$_\n", @$info;
}

All warnings aside, the syntax to use symbolic references should you need to use it (but you won’t) is:

use strict;
use warnings;

my $name="bob";

our @bob = qw/jerk genius/;

my $qualities;

{   no strict 'refs';
    print "$name: ", @$name, "\n";
    $qualities = \@$name;
}

print "$name is a @$qualities\n";  

Note that the array @bob is declared with our. Symbolic references only work with values in the symbol table. In other words, lexical variables do not work with symbolic references.

Just in case I haven’t emphasized this enough, don’t use symbolic references.

Leave a Comment