What recommended Perl modules can serialize Moose objects?

I usually used Storable with nstore , but now I have a module that has CODE and apparently Storable doesn't like that.

I found YAML (and YAML::XS , which I really can't work ). I also experimented a bit with MooseX :: Storage without much success.

Are there other alternatives? What would you recommend?

+3
source share
3 answers

You can reset the code from Data :: Dumper after setting $Data::Dumper::Deparse to the true value, but this is only for debugging purposes, not serialization.

I would advise you to return to the question of why MooseX :: Storage does not work for you, since the authors tried very hard to present a well-functioning and reliable solution for serializing Moose objects.


Update: it looks like you are facing problems that serialize the _offset_sub attribute as described in this question . Since this attribute has a builder, and its construction is pretty trivial (it just looks at the current value of another attribute), you do not need to serialize it at all - when you deserialize your object and want to use it again when you call $this->offset , the builder will be called upon the first call. Therefore, you should simply mark it as "do not serialize":

 use MooseX::Storage; has '_offset_sub' => ( is => 'ro', isa => 'CodeRef', traits => [ 'DoNotSerialize' ], lazy => 1, builder => '_build_offset_sub', init_arg => undef, ); 

Finally, this is somewhat orthogonal, but you can reset the offset and _offset_sub attributes together using the native attribute 'Code' attribute:

 has offset => ( is => 'bare', isa => 'CodeRef', traits => [ qw(Code DoNotSerialize) ], lazy => 1, builder => '_build_offset', init_arg => undef, handles => { offset => 'execute_method', }, ); sub _build_offset { my ($self) = @_; # same as previous _build_offset_sub... } 
+6
source

Check out KiokuDB , its design with and for Moose , so it should really cover all angles (NB. I have not tried it myself, but I keep the point!)

/ I3az /

+3
source

I believe Data :: Dump :: Streamer can serialize coderefs. Didn't use it myself, though.

+1
source

All Articles