Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

variables - How do I use symbolic references in Perl?

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";
@bob = ("jerk", "perlfan");

how should I use $name to get to know what kind of person bob is? Although I'm not quite sure, my vague memory tells me it might related to typeglob.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
";
    print map "$_
", @$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, "
";
    $qualities = @$name;
}

print "$name is a @$qualities
";  

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...