【问题标题】:Can we use Storable's store method on a tied hash?我们可以在绑定哈希上使用 Storable 的存储方法吗?
【发布时间】:2013-11-07 11:57:51
【问题描述】:

尝试将哈希引用存储在 Storable 文件中,它工作正常。但是,我还需要按排序顺序保存键 - 所以我使用了以下

tie %$hashref, 'Tie::IxHash'; store $hashref, $filename;

但这不起作用 - 文件被创建但它只有 50 个字节的大小,当我使用retrieve() 时,我得到一个空哈希。

我尝试使用 Tie::IxHash::Easy(因为我的哈希是哈希的哈希)但无济于事。有什么帮助吗?

【问题讨论】:

    标签: perl hash storable


    【解决方案1】:

    你需要在填充之前绑定 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
            };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-23
      • 2013-03-12
      • 2013-11-10
      相关资源
      最近更新 更多