in reply to Hooks like Storable for Dumper?

Data::Dumper has limited support for this. It requires a single freeze/thaw method for all objects being dumped. Older versions would insist that the method actually existed for all the objects being dumped. It also (afair) suffers the problem that it leaves the objects in their frozen state after the dump.

Data::Dump::Streamer has more sophisticated support for dumping and thawing objects in various ways. Including support for different freeze/thaw methods per class as well as allowing the object to be able to provide a totally seperate proxy when it is frozen. DDS, does NOT leave the objects in a frozen state when it dumps. I will admit that the documentation of this isnt so hot, but id happy to decrypt my mutterings on the subject as required. (I've never received any feedback on this particular issue tho.)

I like to think that I made Data::Dump::Streamer a lot more flexible in this area than either Storable or Data::Dumper. Which is not to mention all the other positives from using DDS. :-) (Of course it does have the disadvantage that it only has a Perl-XS hybrid implementation, but if compiling isnt a problem then DDS should work fine out of the box.)

Update: For those that can't be bothered to look here is the documentation from DDS on this subject:

Controlling Object Representation (Freeze/Thaw)

This module provides hooks for specially handling objects. Freeze/Thaw for generic handling, and FreezeClass/ThawClass for class specific handling. These hooks work as follows (and it should be understood that Freeze() below refers to both it and FreezeClass as does Thaw() refer to ThawClass() as well.

If a Freeze() hook is specified, then it is called on the object during the Data() phase prior to traversing the object. The freeze hook may perform whatever duties it needs and change its internal structure, _or_ it may alter $_[0] providing a substitute reference to be dumped instead (note that this will not alter the data structure being dumped). This reference may even be a totally different type!

If a Thaw() hook is specified then as part of the dump code it will be included to rebless the reference and then call the hook on the newly created object. If the code was originally frozen (not replaced) the method will be called on the object to unfreeze it during the Out() phase of the dump, leaving the structure unmodified after the dump. If the object was replaced by the freeze hook this doesn't occur as it is assumed the data structure has not changed. A special rule applies to Thaw() hooks in that if they include the prefix "->" then they are not executed inline, and as such expected to return the object, but as an independent statement after the object hash been created created, and the return of the statement is ignored. Thus a method that simply changes the internal state of the object but doesn't return an object reference may be used as a Thaw() handler.

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.

Note that the Freeze/Thaw methods will NOT be executed on objects that don't support those methods. The setting in this case will be silently ignored.

BTW, I presented code to implement a serializable insideout object framework in Yet Another Perl Object Model (Inside Out Objects). Why i never uploaded it to CPAN O dont remember.

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

Replies are listed 'Best First'.
Re^2: Hooks like Storable for Dumper?
by xdg (Monsignor) on Jan 03, 2006 at 15:22 UTC

    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.

      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.