Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Storable alternatives

by salva (Abbot)
on May 17, 2013 at 11:16 UTC ( #1033962=perlquestion: print w/replies, xml ) Need Help??
salva has asked for the wisdom of the Perl Monks concerning the following question:

Are there any good alternatives to Storable?

I need a serializer that can be instructed to replace some inner objects on the fly just before serializing then using a callback.

Storable has the STORABLE_freeze and STORABLE_thaw hooks but they are not per-object but per-class and besides that, they are methods of the class, not something that can be controlled by the code calling the serializer.

I was thinking of extending Storable, but frankly, its code its one of the worst examples of code rot I have ever seen and so, my question.

update: an example of what I need to do:

my $data = serialize $tree, sub { my $obj = shift; if (check_some_condition($obj)) { return replace($obj) } return (); # serialize $obj as is };

Replies are listed 'Best First'.
Re: Storable alternatives
by tobyink (Abbot) on May 17, 2013 at 13:59 UTC

    Sereal may be worth looking at. It doesn't have the hooks you're looking for, but based on my experience (porting Sereal::Encoder to PHP) it's relatively clean code if you want to extend it. (Albeit mostly XS.)

    It's also an actively developed framework and format, so you might have a good chance of getting your extensions adopted into the Sereal::{Encoder,Decoder} modules.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Storable alternatives
by BrowserUk (Pope) on May 17, 2013 at 17:09 UTC

    If you decide to implement this yourself, you could do worse than use Clone as a starting point.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Storable alternatives
by BillKSmith (Vicar) on May 17, 2013 at 14:30 UTC

    The book "Intermediate Perl" has a rather long section on "Marshalling Data". It may help.

Re: Storable alternatives
by vsespb (Chaplain) on May 17, 2013 at 13:06 UTC
    What's stopping you from using procedural programming?
    sub my_serialize { my $obj = shift; if (check_some_condition($obj)) { return freeze replace($obj) } return freeze $obj; # serialize $obj as is }; my $data = my_serialize($tree)
      $tree may be an arbitrarily complex structure and I just want to replace some of the objects inside at serialization time.

      Your code can only be used to replace the top object.

        Your code can only be used to replace the top object.
        Not quite understand. replace() sub here _can_ replace just _some_ of the inner objects of $tree
Re: Storable alternatives
by sundialsvc4 (Abbot) on May 17, 2013 at 13:22 UTC

    Storable has produced some very interesting .. ahem .. problems for me that I never quite figured out.   I would freeze something, then thaw it again, and it came out corrupted.   Some did, some didn’t.   I don’t know if it was the Perl version, the system (an AS/400), or what.   But what I did find, that worked out equally well for me (given that storage-space was not a concern), was good ol’ JSON, stored in a memo-field in a database.   (Which had the added bonus of being human-readable.)   I’ve worked with plenty of legacy systems since then which seem to have no problems with it, but I’ve become gun-shy.

      Unfortunately, JSON can only represent a subset of the data structures that can be created in Perl. In example, it can not represent self-referencing data structures.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1033962]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-05-25 20:49 GMT
Find Nodes?
    Voting Booth?