Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Storable reports Can't store CODE items at blib\lib\Storable.pm (autosplit into blib\lib\auto\Storable\_store.al) line 264

if you attempt to store an object that contains a coderef.

Can anything else handle this?

Replies are listed 'Best First'.
Re: Saving/restoring coderefs.
by Corion (Patriarch) on Sep 08, 2007 at 12:27 UTC

    Data::Dumper and Data::Dump::Streamer can, at least for plain code references that are not closures (or, to be more precise, are not closing over lexical variables outside their scope). Of course, that doesn't help you much, since you then have either two formats, the compact format of Storable and the less compact formats of Data::Dumper or DDS. Also, if you have arbitrary, non-trivial callbacks, there is a good chance that you won't be able to properly restore them, and the differences are subtle (but I have difficulties with coming up with an example where it's not obvious why the one does work and the other doesn't, so maybe it will always work like you expect if you manage to have the right expectations). The critical point is whether you can serialize/deserialize all callbacks in one go:

    { # these two subroutines and $i must be serialized in one go # so the dumper knows that the $i++ means the same variable # in both code structures: my $i; sub increment { $i++ }; sub decrement { $i-- }; };
Re: Saving/restoring coderefs.
by radiantmatrix (Parson) on Sep 12, 2007 at 20:29 UTC

    The YAML::Syck and YAML modules can store coderefs, with some configuration; however, you may wish to consider the implications of serializing code -- it will be eval'd on the way back in. Anyone who can edit the file in production (even if they can't edit your application source) will be able to execute arbitrary code by doing so.

    I'm not saying necessarily that you don't want this behavior, but I am saying that you should be really sure that's what you want.

    <radiant.matrix>
    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet