Implementing Storable's STORABLE_freeze and STORABLE_thaw

Discussion in 'Perl Misc' started by senneseph@gmail.com, Jul 13, 2005.

  1. Guest

    Hello Perl People,
    I'm interested in cloning some Perl objects that I've written, but
    I have hit a wall in the documentation and have exhausted Google's
    wealth of knowledge looking for good examples (I can't even find a
    solid example in the Perl modules I have installed). Specifically, I'm
    looking for any kind of elaboration about the "$serialized" return
    value of Storable's STORABLE_freeze hook (also the third argument of
    the STORABLE_thaw hook). What does $serialized look like? I know it's
    a string, but in what format? Do I pick the format? Is there a
    "recommended" or popular format? Also, if anyone knows of any gotchas
    using Storable on ObjectTemplate-derived classes, I'd love to know
    them.
    If I'm asking this question in the wrong forums, feel free to
    nudge me in the right direction.
    , Jul 13, 2005
    #1
    1. Advertising

  2. wrote in
    news::

    > I'm interested in cloning some Perl objects that I've written,
    > but I have hit a wall in the documentation and have exhausted Google's
    > wealth of knowledge looking for good examples (I can't even find a
    > solid example in the Perl modules I have installed). Specifically,
    > I'm looking for any kind of elaboration about the "$serialized" return
    > value of Storable's STORABLE_freeze hook (also the third argument of
    > the STORABLE_thaw hook).


    In all seriousness, do you know why those hooks are there?

    They enable you to override Storable's methods for serializing objects.
    Incidentally, the documentation for Storable states:

    <quotation>
    Unless you know better, serializing hook should always say:

    sub STORABLE_freeze {
    my ($self, $cloning) = @_;
    return if $cloning; # Regular default serialization
    ....
    }

    in order to keep reasonable dclone() semantics.
    </quotation>

    so, I do not understand why you want to use the hook for cloning.

    > What does $serialized look like?


    If you want to override the functionality provided by Storable, you'll
    need to decide how you want to serialize the objects. This question
    suggests to me that you do not understand why those hooks are there, and
    makes your objective suspect.

    > I know it's a string, but in what format? Do I pick the format?


    See above.

    > Is there a "recommended" or popular format?


    The one(s) provided by default by Storable.

    > If I'm asking this question in the wrong forums, feel free to
    > nudge me in the right direction.


    This is the right place to ask, but you will need to put some effort
    into framing a coherent question. In particular, I recommend reading the
    posting guidelines for this group, as well as

    <URL:http://www.catb.org/~esr/faqs/smart-questions.html>

    The likelihood of getting useful answers increases exponentially if you
    post short, self-contained code to accompany your questions.

    Sinan


    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 13, 2005
    #2
    1. Advertising

  3. Guest

    wrote:
    > Hello Perl People,
    > I'm interested in cloning some Perl objects that I've written, but
    > I have hit a wall in the documentation and have exhausted Google's
    > wealth of knowledge looking for good examples (I can't even find a
    > solid example in the Perl modules I have installed).


    Are you sure you can't just let Storable do it all for you? If you can't,
    why can't you?

    > Specifically, I'm
    > looking for any kind of elaboration about the "$serialized" return
    > value of Storable's STORABLE_freeze hook (also the third argument of
    > the STORABLE_thaw hook). What does $serialized look like? I know it's
    > a string, but in what format?


    Whichever format conveys the necessary information to let you unfreeze
    the object.

    > Do I pick the format? Is there a
    > "recommended" or popular format?


    The popular format is the one that Storable uses when you let it do its
    thing without trying to implement it yourself. If you are doing something
    that exceeds the default ability of Storable, then you are inherently on
    rather esoteric ground, and so something non-popular is probably required.
    If I had to do this, I'd probably return a empty or dummy $serialized
    and put whatever I needed for serialization into the list of refs that
    come after it.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jul 14, 2005
    #3
  4. Guest

    I must have missed a dose of caffeine when I wrote "I'm interested in
    cloning some Perl objects..." because I meant to say "serializing", not
    "cloning". Sorry for the confusion.

    <code>
    package Gnarly;
    use strict;
    use Class::ObjectTemplate;
    @ISA = ('ObjectTemplate');
    attributes qw(foo bar);

    sub STORABLE_freeze { # $object, $cloning
    my ($this, $cloning) = @_;
    return if $cloning;

    my $serialized;
    # do what to $serialized;
    # $serialized = "foo:$this->foo()|...."?

    return $serialized;
    }

    sub STORABLE_thaw { # $object, $cloning, $serialized
    my ($object, $cloning, $serialized) = @_;

    # do something to $serialized? #
    return;
    }
    </code>
    , Jul 14, 2005
    #4
  5. wrote in
    news::

    [ Please quote an appropriate amount of context when replying.
    Please see <URL:http://tinyurl.com/c68ld> for more information.
    ]

    > I must have missed a dose of caffeine when I wrote "I'm interested in
    > cloning some Perl objects..." because I meant to say "serializing",
    > not "cloning". Sorry for the confusion.


    OK

    > <code>
    > package Gnarly;
    > use strict;
    > use Class::ObjectTemplate;
    > @ISA = ('ObjectTemplate');
    > attributes qw(foo bar);
    >
    > sub STORABLE_freeze { # $object, $cloning
    > my ($this, $cloning) = @_;
    > return if $cloning;
    >
    > my $serialized;
    > # do what to $serialized;
    > # $serialized = "foo:$this->foo()|...."?


    Well, since you are the one who wants to customize the behavior of
    Storable, you ought to know *how* you want to customize it, right?

    I am going to give this a shot, because I would like to learn something
    today. It is likely going to be a flaky implementation.

    I am going to suppose you want to serialize to a simple XML format.

    #!/usr/bin/perl

    package My::StorableObject;

    use Class::ObjectTemplate;
    use XML::Simple;

    use strict;
    use warnings;

    use base qw'Class::ObjectTemplate Exporter';
    attributes( qw(one two) );

    sub STORABLE_freeze {
    my ($obj, $cloning) = @_;
    return if $cloning;

    my @attrs = $obj->get_attribute_names;
    my %attrs = map { $_ => $obj->get_attributes($_) } @attrs;
    return XMLout(\%attrs);
    }

    sub STORABLE_thaw {
    my ($obj, $cloning, $serialized) = @_;
    return if $cloning;

    print $serialized."\n";

    my $attrs = XMLin($serialized);

    for my $attr (keys %$attrs) {
    $obj->set_attribute($attr, $attrs->{$attr});
    }
    }

    package main;

    use Storable qw'freeze thaw';

    my $obj = My::StorableObject->new;
    $obj->one(1);
    $obj->two(2);

    my $s = freeze $obj;

    my $o2 = thaw $s;

    print join("\n", $o2->one, $o2->two)."\n";

    __END__



    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 14, 2005
    #5
  6. "A. Sinan Unur" <> wrote in
    news:Xns9692CC95BC52Easu1cornelledu@127.0.0.1:

    > sub STORABLE_thaw {
    > my ($obj, $cloning, $serialized) = @_;
    > return if $cloning;
    >
    > print $serialized."\n";


    This print is unnecessary, and should be deleted.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 14, 2005
    #6
  7. Guest

    A. Sinan Unur,
    A working example is really much more than I was expecting; I
    thank you for your effort. I didn't realize $serialized could just be
    any arbitrary format as long as it gets encoded and decoded properly.
    I thought that maybe Storable wanted the data in a special format of
    some kind. XML::Simple certainly does make things simple.
    , Jul 14, 2005
    #7
  8. wrote in news:1121349099.594080.192160
    @o13g2000cwo.googlegroups.com:

    > A. Sinan Unur,
    > A working example is really much more than I was expecting; I
    > thank you for your effort. I didn't realize $serialized could just be
    > any arbitrary format as long as it gets encoded and decoded properly.
    > I thought that maybe Storable wanted the data in a special format of
    > some kind. XML::Simple certainly does make things simple.


    Well, you are welcome. However, the best way to show your appreciation
    is to stick with the posting conventions we use here.

    One of these is to quote an *appropriate* amount of context when you are
    replying.

    Please do read <URL:http://tinyurl.com/c68ld>

    Since then, my killfile has grown exponentially, mostly in response to
    postings from Google Groups.

    If you do not follow the conventions, you risk being ignored by a lot of
    people here.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 14, 2005
    #8
  9. wrote in news:1121349099.594080.192160
    @o13g2000cwo.googlegroups.com:

    [ regarding over-riding STORABLE_freeze and STORABLE_thaw ]

    > I didn't realize $serialized could just be any arbitrary format
    > as long as it gets encoded and decoded properly.


    That is what it means to be able to customize the behavior. You could
    encode the data in morse code for all Storable cares.

    > XML::Simple certainly does make things simple.


    I want to be 100% clear: I am not recommending you actually use my example
    in production code. There is likely going to be a substantial memory and
    speed hit, compared to what Storable does by default.

    Given that you want to customize Storable's behavior, you ought to know
    why you want to customize it, and therefore how you want to customize it.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 14, 2005
    #9
  10. Guest

    wrote:
    > I must have missed a dose of caffeine when I wrote "I'm interested in
    > cloning some Perl objects..." because I meant to say "serializing", not
    > "cloning". Sorry for the confusion.



    I'm not sure, but you seem to think that it is mandatory
    to have STORABLE_freeze and STORABLE_thaw methods in order for Storable
    to work on an object. It is not. If you do not define those methods,
    then Storable will do the serialization for you according to its own
    best judgement, so to speak. You only need to make these methods if you
    are not happy with Storable's default behavior.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jul 14, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Hembree
    Replies:
    0
    Views:
    1,191
    Daniel Hembree
    Jan 12, 2004
  2. Harsha
    Replies:
    1
    Views:
    571
  3. Replies:
    3
    Views:
    2,429
    Jim Gibson
    Mar 29, 2005
  4. Matija Papec

    Storable and tied variables

    Matija Papec, Jan 11, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    122
    Matija Papec
    Jan 11, 2011
  5. arun
    Replies:
    1
    Views:
    1,393
    Rainer Weikusat
    Mar 8, 2012
Loading...

Share This Page