in reply to Avoiding DBI handles with Storable

I still don't get what the return value of the STORABLE_freeze function does to anything (other than returning an empty list to nullify the subroutine, I can't get the return value to do anything; maybe its broken?), but I've been messing with freeze/thaw hooks and here's what I have which may help (Maybe the secret is to just thaw it the way you want?):
#!/usr/local/bin/perl -l -w use strict; use Storable; use Data::Dumper; package MyPack; sub new { my $class = shift; bless {attrib=>"red"}, $class; } sub STORABLE_freeze { my ($self, $cloning) = @_; return if $cloning; return $self; } # $self always seems to be an empty object # blessed into the class, so lets re-bless it. sub STORABLE_thaw { my ($self, $cloning) = @_; return if $cloning; bless $self, 'NoPackage'; $self->{mykey} = "value"; return $self; } package main; my $obj = MyPack->new; my $thing = { a=>"abc", b=>$obj }; print Dumper($thing); store $thing, 'store.tmp'; my $another_thing = retrieve('store.tmp'); print Dumper($another_thing); Output: $VAR1 = { 'a' => 'abc', 'b' => bless( { 'attrib' => 'red' }, 'MyPack' ) }; $VAR1 = { 'a' => 'abc', 'b' => bless( { 'mykey' => 'value' }, 'NoPackage' ) };

Replies are listed 'Best First'.
Re: Re: Avoiding DBI handles with Storable
by Fastolfe (Vicar) on Jan 31, 2001 at 05:05 UTC
    When Storable comes across a reference blessed into a class, it examines that class to see if there is a 'STORABLE_freeze' method (and _thaw, I guess, but that's out of the scope of this question) that can produce a "serialized" version of that object. If it exists, according to the documentation for Storable, it's to return a serialized version of the object. If no such method exists (or if the first invocation of this method returns an empty list), Storable reverts to what it normally does.

    I am just attempting to define a STORABLE_freeze method for the DBI database handle (which seems to be of the DBI::db class, but I haven't been able to investigate past this point yet), which returns a garbage/no-op serialization for the database handle, much like what Storable does by default when you set $Storable::forgive_me and attempt to serialize a coderef or a file handle. Make sense?

    Your code seems to work as-is, though, without error (though your _thaw routine is non-sensical from a Storable point of view, as it does nothing to restore the value that was saved, but that doesn't matter), so I'm wondering if I'm doing something wrong at a more fundamental level...