views:

45

answers:

1

I have a few Moose objects and some other simple hash objects (hashes, arrays) I'd like to serialize.

At first, I used a simple

my $obj_store_file = nstore($obj);

and

my $obj = retrieve($obj_store_file);

This worked well.

Later, I found about MooseX::Storage and KiokuDB. I tried using them to enjoy some benefits they have, but:

  • MooseX::Storage seemed to recreate objects that are referred multiple times. For example, one of my serialized objects contains a few attributes, which each of them refers to the same instance of another object. Before serialization, all of these reference are obviously the same -- they all point to the same object. After serialization/de--serialization using MooseX::Storage, this once single object is duplicated and each reference points to another instance of the object. I was told that MooseX::Storage is not appropriate to represent object graphs and that I might want to try KiokuDB.
  • I did, although I felt KiokuDB is an overkill for my needs. I don't need all the fancy stuff a DB can offer. Unfortunately, since one of my objects is really large and choaks on memory when serialized using defaults, it seems I have to write a custom serializer or store its 'data' portion separately then write a costume KiokuX::Module... again, quite an overkill.

So, I'm back to plain ol' Storable or YAML. My question is simple: yes, there are some benefits for KiokuDB (especially the fact it maintains an object graph) and perhaps also for MooseX::Storage (although I couldn't really find any for the latter). But, given these benefits are not really of use to me, is there any reason not to use Storable or YAML?

In other words, is there anything wrong with storing a (Moose) object this way? Is it 'illegal'?

+1  A: 

My experience is that it depends on why you're serializing data. I like Storable for program state including things like window size/position. I prefer YAML for configuration data or anything you might want to exchange with another copy of the application. (i.e. share between users -- a Storable file might not be readable by a user with a different version of Perl or Storable.) Storable supports object graphs (assuming that the freeze/thaw is done correctly). I'm not sure about YAML.

Michael Carman
I'm storing the objects just for my own later use. I do not want to recreate them each time (some of them require some heavy preprocessing). What I like about YAML is the ability to look at the stored file itself. It's nice to see what's going on in there. It seems YAML also supports object graphs - it uses internal pointers, but what bothers me is why won't it auto-load the needed classes.
David B