如何在Perl中使类的成员成为哈希?

前端之家收集整理的这篇文章主要介绍了如何在Perl中使类的成员成为哈希?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试用perl编写一个包.我需要其中一个成员成为哈希.但是,当我引用并运行程序时,我无法使用通常的语法.如果我有:

sub new
{
my $class = shift;
my $self = {
    textfile => shift,placeholders => ()
};
bless $self,$class;
return $self;
}

有没有办法让“占位符”成为我可以通过$self-> {placeholders}访问的哈希?

谢谢

解决方法

是的,但您必须将其作为哈希引用.

$self = {
   textfile => shift,placeholders => { }         #  { },not ( )
};
...


$self->{placeholders}->{$key} = $value;
delete $self->{placeholders}->{$key};
@keys = keys %{$self->{placeholders}};
foreach my ($k,$v) each %{$self->{placeholders}} { ... }
...

猜你在找的Perl相关文章