4

I was usually using 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 can't really get to work). I also experimented a bit with MooseX::Storage without much success.

Are there other alternatives? What would you recommend?

Community
  • 1
  • 1
David B
  • 29,258
  • 50
  • 133
  • 186
  • MooseX::Storage is the recommended method. Can you be specific about the issues you are encountering with it? (Feel free to pop onto irc.perl.org #moose as well.) – Ether Oct 22 '10 at 19:18
  • 1
    Why do you want to dump coderefs? – brian d foy Oct 22 '10 at 21:24
  • 1
    @brian d foy: I really don't. See the module linked in the OP. If you have any suggestion how to serialize this object without using coderefs, I'll be happy to adopt it. – David B Oct 22 '10 at 22:45
  • @Ether: for starters, `MooseX:Storage` dors not support coderefs (http://search.cpan.org/~bobtfish/MooseX-Storage-0.28/lib/MooseX/Storage.pm#What_can_not_be_serialized?) – David B Oct 22 '10 at 22:47
  • 1
    I meant my question much more in the sense of "Tell us what you are doing and why you think this is a solution". There's no context for your question. You've gotten enough feedback here to know you should be telling us what you're doing rather than isolating what you think the issue is. If this is really just the same issue that you've already asked, choose one question and stick with it instead of duplicating effort. – brian d foy Oct 22 '10 at 23:03
  • @brian d foy: please see my module linked in the OP. This explains exactly what I'm doing (the module is right there. and I simply wish to serialize its instances). It's not the same question, but the same module. I was previously asked to separate questions and not ask a few things together even if they relate to the same piece of code, and I try to follow this rule. – David B Oct 22 '10 at 23:07
  • @David: which original post? there's one from a few weeks ago where you ask how to serialize closures, where I responded "Perhaps it is time to step back and consider whether serializing subs is a good idea?" But I don't see a question where the answer includes "you need to serialize your entire Moose object, including its methods". – Ether Oct 22 '10 at 23:47

3 Answers3

6

You can dump a coderef with Data::Dumper after setting $Data::Dumper::Deparse to a true value, but this is only intended for debugging purposes, not for serialization.

I would suggest you go back to looking at why MooseX::Storage isn't working out for you, as the authors tried really hard to present a well-abstracted and robust solution for Moose object serialization.


Update: it looks like you are running into issues serializing the _offset_sub attribute, as described in this question. Since that attribute has a builder, and its construction is fairly trivial (it just looks at the current value of another attribute), you shouldn't need to serialize it at all -- when you deserialize your object and want to use it again, the builder will be invoked the first time you call $this->offset. Consequently, you should just be able to 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,
);

Lastly, this is somewhat orthogonal, but you can fold the offset and _offset_sub attributes together by using the native attribute 'Code' trait:

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...
}
Community
  • 1
  • 1
Ether
  • 53,118
  • 13
  • 86
  • 159
3

Have a look at KiokuDB, its designed with and for Moose so it should really cover all the corners (NB. I haven't tried it myself but I keep meaning to!)

/I3az/

draegtun
  • 22,441
  • 5
  • 48
  • 71
  • 1
    I'm currently giving a look at `KiokuDB`. It seems interesting, but I actually need to serialize a single object or only a couple of objects at time. I do not need smart searching etc. Using DB backends etc. seems like an overkill for my case. – David B Oct 22 '10 at 23:50
  • KiokuDB is really not a serialization engine (although it has one) but an Object Store. – perigrin Feb 18 '11 at 20:56
1

I believe Data::Dump::Streamer can serialize coderefs. Haven't used it myself though.

Eric Strom
  • 39,821
  • 2
  • 80
  • 152
  • It can, and works okay as long as you understand its documented caveats. I'd avoid dumping coderefs if possible though. – brian d foy Oct 22 '10 at 21:24