尝试将散列引用存储在可存储文件中并且工作正常。但是,我还需要按排序顺序保存键 - 所以我使用了以下内容
tie %$hashref, 'Tie::IxHash';
存储 $hashref, $filename;
但这不起作用 - 文件已创建,但其大小只有 50 个字节,当我使用 retrieve() 时,我得到一个空散列。
我尝试使用 Tie::IxHash::Easy(因为我的哈希是哈希的哈希)但无济于事。有帮助吗?
最佳答案
您需要在填充之前绑定(bind) hashref
use strict; use warnings;
use Storable;
use Tie::IxHash;
use Data::Dumper;
my $filename= 'xxx';
my $hashref;
tie %{$hashref}, 'Tie::IxHash';
$hashref->{b}={c=>2, d=>{e=>3}};
$hashref->{a}=1;
print Dumper(before=>$hashref);
store $hashref, $filename;
print Dumper(after=>retrieve('xxx'));
返回
$VAR1 = 'before';
$VAR2 = {
'b' => {
'c' => 2,
'd' => {
'e' => 3
}
},
'a' => 1
};
$VAR1 = 'after';
$VAR2 = {
'b' => {
'c' => 2,
'd' => {
'e' => 3
}
},
'a' => 1
};
关于perl - 我们可以在绑定(bind)哈希上使用 Storable 的存储方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19835334/