Why will Storable load some classes, but not others?

279 Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

DateTime defines STORABLE_freeze and STORABLE_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. The module loading is part of the hook logic which is not invoked for Foo.

1
On

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.