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


In reply to Re: Hooks like Storable for Dumper? by demerphq
in thread Hooks like Storable for Dumper? by xdg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.