Implementing Storable's STORABLE_freeze and STORABLE_thaw

S

senneseph

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.
 
A

A. Sinan Unur

(e-mail address removed) wrote in
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
 
X

xhoster

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
 
S

senneseph

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>
 
A

A. Sinan Unur

(e-mail address removed) wrote in

[ Please quote an appropriate amount of context when replying.
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

A. Sinan Unur

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

print $serialized."\n";

This print is unnecessary, and should be deleted.

Sinan
 
S

senneseph

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.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @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

A. Sinan Unur

(e-mail address removed) wrote in @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
 
X

xhoster

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top