【发布时间】:2018-06-25 07:39:53
【问题描述】:
假设我正在处理另一个包中的哈希,并且我想为该变量创建一个别名?有什么干净的方法可以做到这一点?这个示例代码显示了我想要做什么,但是它以及它的十几个变体都出现了故障。
#!/usr/bin/perl
#use strict; # I want it to work with strict.
my $p = Person->new("Joe");
my $name;
*name = \*$p->{"name"}; # Latest broken attempt
printf("'%s' should eq '%s' because they both point to the same place.\n",
$name, $p->{"name"});
$name = "Sam";
printf("'%s' should eq '%s' because they both point to the same place.\n",
$name, $p->{"name"});
exit 0;
package Person;
sub new {
my $class = shift;
my $this = {};
bless $this, $class;
$this->{"name"} = shift;
return $this;
}
我想要的功能是能够操作$p->{"name"} 几十次,而不必重复输入$p->{"name"}。真正的数据结构要复杂得多,有很多嵌套的哈希。
这可能很容易。 谢谢。
【问题讨论】:
标签: perl alias perl-module