In Perl, if a variable holds the name for another variable, how do I use the first variable to visit the other one?
For example, let
$name = \"bob\";
Several points:
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.