如何在Perl中使用符号引用? [英] How do I use symbolic references in Perl?

查看:102
本文介绍了如何在Perl中使用符号引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中,如果一个变量拥有另一个变量的名称,我该如何使用第一个变量访问另一个变量?

例如,让

$name = "bob";
@bob = ("jerk", "perlfan");

我应该如何使用$ name来了解鲍勃是什么样的人? 尽管我不太确定,但我记忆模糊的内容告诉我,这可能与typeglob有关.

解决方案

几点:

  • 您不是在谈论typeglob,而是在谈论符号引用.
  • 不要使用符号引用-它们会导致难以跟踪错误.
  • 在几乎所有符号引用似乎是一个好主意的情况下,使用基于哈希的数据结构都是最好的方法.
  • 当您不想更改哈希值时,请考虑使用Hash :: Util锁定哈希值.
  • 符号引用不适用于词汇变量.
  • Typeglob在词法变量上不起作用.
  • 使用词汇变量.
  • 不要使用符号引用.

有关参考(符号或其他)的更多信息,请参见 perlreftut . 有关使用数据结构的帮助,请参见 perldsc . 有关typeglob的更多信息,请参见 perlmod . 有关词法变量的更多信息,请参见 perlsub .

这是一个使用锁定的哈希值基于变量内容控制对数据的访问的示例:

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;
}

除了所有警告,如果需要使用符号引用,则使用语法(但不必),语法为:

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";  

请注意,数组@bobour声明.符号引用仅适用于符号表中的值.换句话说,词汇变量不适用于符号引用.

以防万一我没有足够强调, 不要使用符号引用 .

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.

解决方案

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.

这篇关于如何在Perl中使用符号引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆