3

I'm having a problem with frozen objects in Storable. When Storable thaws an object, it's supposed to load the class. 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.

$ 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. The object is restored, but Foo.pm is not loaded. I have this problem with Storable 2.34 and perl 5.16.2.

Can people repeat this problem? Is there a solution?

4

2 回答 2

3

DateTime 定义STORABLE_freezeSTORABLE_thaw。可以推断 Storable 记录了该类是否使用了一个钩子来冻结自己并寻找相应的钩子来解冻它。模块加载是 Foo 不调用的钩子逻辑的一部分。

于 2013-04-20T02:27:01.267 回答
-1

你总是可以做

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

不确定您看到的 DateTime 的行为是否是 DateTime 的 Storable-hooks 的结果,但您不需要挂钩来要求模块。

于 2013-04-20T16:46:31.763 回答