gpt4 book ai didi

perl - 为什么 Storable 会加载一些类,而不加载其他类?

转载 作者:行者123 更新时间:2023-12-02 02:03:26 24 4
gpt4 key购买 nike

我在 Storable 中遇到卡住对象的问题。当 Storable 解冻一个对象时,它应该加载该类。这通常是正确的,但有时并非如此。这是一些示例代码...

#!/usr/bin/env perl -l

use strict;
use warnings;

use Storable;

if( fork ) {
}
else {
print "In child.";

# Load modules in a child process so the parent does not have them loaded
require Foo;
print $INC{"Foo.pm"};
print $Foo::VERSION;
Storable::store( Foo->new("http://example.com"), "/tmp/tb2.out" );

require DateTime;
print $INC{"DateTime.pm"};
Storable::store( DateTime->new(year => 2009), "/tmp/uri.out" );

exit;
}

wait;

print "Child is done.";
print "------------------------------";

print "DateTime is not loaded" if !$INC{"DateTime.pm"};
my $datetime = Storable::retrieve("/tmp/uri.out");
print $INC{"DateTime.pm"};
print $datetime->year;

print "Foo is not loaded" if !$INC{"Foo.pm"};
my $obj = Storable::retrieve("/tmp/tb2.out");
print $INC{"Foo.pm"};
print $obj->id;

还有非常简单的 Foo.pm。

$ cat lib/Foo.pm 
package Foo;

use strict;
use vars qw($VERSION);
$VERSION = "1.60";

sub new {
my $class = shift;

return bless { foo => 23 }, $class;
}

sub id { 42 }

1;

我从那个程序中得到...

In child.
lib/Foo.pm
1.60
/Users/schwern/perl5/perlbrew/perls/perl-5.16.2-threads/lib/site_perl/5.16.2/darwin-thread-multi-2level/DateTime.pm
Child is done.
------------------------------
DateTime is not loaded
/Users/schwern/perl5/perlbrew/perls/perl-5.16.2-threads/lib/site_perl/5.16.2/darwin-thread-multi-2level/DateTime.pm
2009
Foo is not loaded
Use of uninitialized value in print at /Users/schwern/tmp/test.plx line 38.

Can't locate object method "id" via package "Foo" at /Users/schwern/tmp/test.plx line 39.

如您所见,它会愉快地加载 DateTime 而不是我的 Foo 模块。对象已恢复,但未加载 Foo.pm。我在使用 Storable 2.34 和 perl 5.16.2 时遇到了这个问题。

人们可以重复这个问题吗?有解决办法吗?

最佳答案

DateTime 定义了 STORABLE_freezeSTORABLE_thaw。可以推断出,Storable 记录了该类是否使用钩子(Hook)来卡住自身,并寻找相应的钩子(Hook)来解冻它。模块加载是钩子(Hook)逻辑的一部分,不会为 Foo 调用。

关于perl - 为什么 Storable 会加载一些类,而不加载其他类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com