从nstore_fd恢复错误:无法通过包“ Hash :: Case :: Lower”找到对象方法“ FIRSTKEY”

时间:2018-07-18 11:20:40

标签: perl tie storable

我正在研究2005年以来的某个程序。一直很高兴编写这样的备份:

use Storable qw(nstore_fd);  
my %data = 
    (
     "Games" => \%Games,
     "Members" => \%Members,
     "Scenarios" => \%Scenarios,
     "Passwords" => \%Passwords,
     "TourneyNames" => \%TourneyNames
     );

    nstore_fd(\%data, *BACKUP) or die "Can't write backup: $!";

我需要恢复一些数据。

我这样做了:

use Storable qw(fd_retrieve); 

$data = fd_retrieve(*STDIN);  # the backup file 

print keys %{$data}, "\n";

foreach $thing (keys(%{$data})) {
    print "$thing\n";
    print scalar keys %{$data->{$thing}}, "\n";
}

并得到:

$ perl ~/foo.pl < BobsterBackup2018-7-9-35131.bak
GamesMembersPasswordsScenariosTourneyNames
Games
15556
Members
Can't locate object method "FIRSTKEY" via package "Hash::Case::Lower" at /Users/mgregory/foo.pl line 9.
$ 

我看到原始来源有:

tie %Members, 'Hash::Case::Lower';    # index by non-case-sensitive member-name.

...但是我不知所措!

1 个答案:

答案 0 :(得分:4)

还原对象时 tied 要打包,首先需要加载它们的类。 Storable 以及例如 Sereal 不会为您加载它们。它会在存储对象时重新创建它们,而Perl希望这些包存在。

您需要做的就是在反序列化备份之前加载模块。

use Storable qw(fd_retrieve); 
use Hash::Case::Lower;

my $data = fd_retrieve(*STDIN);  # the backup file