in reply to Re: Hooks like Storable for Dumper?
in thread Hooks like Storable for Dumper?

Great reference about serializing inside-out objects. If I give an extended version of the talk at a conference, I'll probably include that.

For now these options are specified as string values representing the method names. Its possible a later version will extend this to also handle codrefs.

I'm still a little fuzzy on DDS's freeze/thaw handlers. Part of your description reads as if DDS might look for a Freeze() method, but the docs read like one has to register the freezing method name via DDS->Freeze (or rather DDS->FreezeClass) first. Could you give a code example?

So I think that's different from what Storable does looking for a STORABLE_freeze method. Any thought of supporting a DDS_freeze method directly if one exists?

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^3: Hooks like Storable for Dumper?
by demerphq (Chancellor) on Jan 03, 2006 at 17:03 UTC

    Regarding the equivelent to Storable, just use the Freeze()/Thaw() method (as per Data::Dumper) to register the method. Im reluctant to hard code the method names as I dont think it scales well.

    use DDS; sub Popsicle::Freeze { my ($self)=@_; $_[0]=bless \do{my $x=join "-",@$self},ref $self; } sub Popsicle::Thaw { my ($self)=@_; $_[0]=bless [ map {split /-/,$_ } $$self ],ref $self; } my $ig=bless ["A".."C"],"Popsicle"; my %h=(One=>1,Two=>2,Three=>$ig); # "fix" statement thaw... Dump->Names('first') ->FreezeClass('Popsicle'=>'Freeze') ->ThawClass('Popsicle'=>'Thaw') ->Data( \%h )->Out; print "\n"; # inline thaw.... Dump->Names('second') ->FreezeClass('Popsicle'=>'Freeze') ->ThawClass('Popsicle'=>'->Thaw') ->Data( \%h )->Out; print "\n"; # clear the hooks for Popsicle Dump->Names('third') ->FreezeClass('Popsicle'=>'') ->ThawClass('Popsicle'=>'') ->Data( \%h )->Out; print "\n"; # Using FreezeThaw to make it a bit easier... Dump->Names('fourth') ->FreezeThaw('Popsicle'=>'Freeze','->Thaw') ->Data( \%h )->Out; print "\n"; # Using generic hooks and not class specific ones Dump->Names('fifth') ->Freeze('Freeze') ->Thaw('->Thaw') ->Data( \%h )->Out; print "\n"; __END__ $first = { One => 1, Three => bless( \do { my $v = 'A-B-C' }, 'Popsicle' ), Two => 2 }; $first->{Three}->Thaw(); $second = { One => 1, Three => bless( \do { my $v = 'A-B-C' }, 'Popsicle' )->Tha +w(), Two => 2 }; $third = { One => 1, Three => bless( [ 'A', 'B', 'C' ], 'Popsicle' ), Two => 2 }; $fourth = { One => 1, Three => bless( \do { my $v = 'A-B-C' }, 'Popsicle' )->Tha +w(), Two => 2 }; $fifth = { One => 1, Three => bless( \do { my $v = 'A-B-C' }, 'Popsicle' )->Thaw +(), Two => 2 };
    ---
    $world=~s/war/peace/g

      Im reluctant to hard code the method names as I dont think it scales well.

      I'd like to better understand your reasoning. One advantage of the Storable approach is that it's defines an interface that modules can implement in order to manage their interoperability with Storable. E.g. Object::InsideOut and Class::Std::Storable.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        Warning this node contains a u-turn of opinion that would impress even a well salted politician...

        I think I finally clicked to why Storable uses the approach it does and why despite the other points raised I think it actually makes more sense. In two words:

        Respect Inheritance!

        The point is that if you define a Storable_freeze() method all Storable has to do is say $obj->can('Storable_freeze') (in a heavy XS accent of course ;-) and it knows if the object needs special handling. With my approach I have to do a hash lookup on the classname, so subclasses won't inherit their freeze/thaw handlers.

        Which brings me to the point of starting to think that my approach is actually subpar and that it makes a lot more sense to copy Storable's approach. Which leaves me in the uncomfortable situation of wanting to support both. Sigh.

        I think that given a classname register would break inheritance I'm going to have to provide DDS_freeze and DDS_thaw support. I guess supporting both approaches has its merits as they are to a certain extent complementary. IE, you could use the existing mechansim to override the DDS_ method hooks if you wanted to.

        Anyway, live and learn I guess.

        ---
        $world=~s/war/peace/g

        I'm glad you challenged me on that. To be clear: I'm not convinced im right in my opinion. Basically my concern is that using such an approach means that you end up with a proliferation of freeze/thaw mechanisms. IE, if somebody wants their class to work with Storable they have one set of method names, if they want their class to work with DDS they would need another set. And if the concept was extended you'd need the same for Data::Dumper and for YAML.

        But maybe this is actually not such a big deal. There arent that many production worthy serialization tools out there, so may the proliferation is managable from the start.

        Alternatively i wonder if maybe DDS should just define a hash so that modules can provide system wide defaults. IE, Class::InsideOut might include a line like:

        $Data::Dump::Streamer::FreezeThaw{__PACKAGE__}=['foo','->bar'];

        If YAML and Data::Dumper used the same approach then you might get code like:

        $Data::Dump::Streamer::FreezeThaw{__PACKAGE__}=['foo','->bar']; $Data::Dumper::FreezeThaw{__PACKAGE__}=['foo','bar']; $YAML::FreezeThaw{__PACKAGE__}=['foo','bar'];

        What do you think? I'm very open to ideas about this. And willing to be convinced that another approach is better.

        ---
        $world=~s/war/peace/g