我正在尝试用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}} { ... } ...