的名单我有一个创建一个文档对象包:的Perl OO - 创建对象
package Document;
sub new
{
my ($class, $id) = @_;
my $self = {};
bless $self, $class;
$self = {
_id => $id,
_title =>(),
_words =>()
};
bless $self, $class;
return $self;
}
sub pushWord{
my ($self, $word) = @_;
if(exists $self->{_words}{$word}){
$self->{_words}{$word}++;
}else{
$self->{_words}{$word} = 0;
}
}
我把它叫做:
my @docs;
while(counter here){
my $doc = Document->new();
$doc->pushWord("qwe");
$doc->pushWord("asd");
push(@docs, $doc);
}
在第一次迭代中,第一$doc
的哈希有两个要素。在第二次迭代中,第二个$doc
的散列有四个元素(包括第一个元素中的两个)。但是,当我使用该实体对象(创建的Document
数组),我得到:
- 文献-1,其中x
- 文献-2的散列大小与x的散列大小+ Y
- 文档 - 3,散列大小为x + y + z
为什么散列大小递增? Document-3具有Document-1和Document-2中的所有散列内容。这是否与祝福或未定义变量有关?构造函数是否错误?
感谢:d
你所说的 “散列大小” 是什么意思? – choroba 2014-11-22 17:36:43
哦,对不起,如果它不明确。我的意思是散列的总元素。因此,Document-2的散列具有Document-1散列的所有元素。 – 2014-11-22 17:39:05
这不是我得到的行为。你的构造函数肯定是错误的,但也请显示你的调用代码。 – Borodin 2014-11-22 17:46:16