简体   繁体   English

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

[英]Why will Storable load some classes, but not others?

I'm having a problem with frozen objects in Storable. 我在Storable有冷冻物体的问题。 When Storable thaws an object, it's supposed to load the class. 当Storable解冻一个对象时,它应该加载该类。 This is usually true, but sometimes it isn't. 这通常是正确的,但有时却不是。 Here's some sample code... 这是一些示例代码......

#!/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;

And the quite simple Foo.pm. 而且相当简单的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;

I get from that program... 我从那个程序中得到......

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.

As you can see, it will happily load DateTime but not my Foo module. 如您所见,它将很乐意加载DateTime而不是我的Foo模块。 The object is restored, but Foo.pm is not loaded. 该对象已恢复,但未加载Foo.pm。 I have this problem with Storable 2.34 and perl 5.16.2. 我有Storable 2.34和perl 5.16.2这个问题。

Can people repeat this problem? 人们可以重复这个问题吗? Is there a solution? 有解决方案吗?

DateTime defines STORABLE_freeze and STORABLE_thaw . DateTime定义了STORABLE_freezeSTORABLE_thaw One could deduce that Storable notes whether the class used a hook to freeze itself and looks for a corresponding hook to thaw it. 人们可以推断出Storable会注意到这个类是否使用了一个钩子来冻结自己并寻找一个相应的钩子来解冻它。 The module loading is part of the hook logic which is not invoked for Foo. 模块加载是钩子逻辑的一部分,不为Foo调用。

You can alway do 你总能这样做

my $class = ref $obj;
eval "require $class";

Not sure whether the bevavior you see for DateTime is a result of DateTime's Storable-hooks, but you don't need a hook to require a module. 不确定你在DateTime看到的bevavior是否是DateTime的Storable-hooks的结果,但你不需要一个钩子来要求一个模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM