哪些推荐的 Perl 模块可以序列化 Moose 对象?

发布于 2024-09-28 19:21:51 字数 456 浏览 7 评论 0原文

我通常将 Storablenstore 一起使用,但现在我有一个 模块CODE ,显然 Storable 不喜欢这样。

我发现 YAML (和 YAML::XS ),我无法真正开始工作)。 我还尝试了一些 MooseX::Storage ,但没有取得太大成功。

还有其他选择吗? 你会推荐什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光与爱终年不遇 2024-10-05 19:21:51

设置 $Data 后,您可以使用 Data::Dumper 转储 coderef ::Dumper::Deparse 为真值,但这仅用于调试目的,不适用于序列化。

我建议您回过头来看看为什么 MooseX::Storage 不适合您,因为作者非常努力地为 Moose 对象序列化提供一个充分抽象且强大的解决方案。


更新:看起来您在序列化 _offset_sub 属性时遇到了问题,如 这个问题。由于该属性有一个构建器,并且它的构造相当简单(它只是查看另一个属性的当前值),因此您根本不需要序列化它——当您反序列化对象并想要再次使用它时,第一次调用 $this->offset 时将调用构建器。因此,您应该能够将其标记为“不序列化”:

use MooseX::Storage;

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

最后,这有点正交,但您可以折叠 offset
使用原生属性“Code”特征将 _offset_sub 属性组合在一起:

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

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...
}
陌上青苔 2024-10-05 19:21:51

看看KiokuDB,它是用Moose 所以它应该真正覆盖所有角落(注意。我自己没有尝试过,但我保留意思是!)

/I3az/

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/

自由如风 2024-10-05 19:21:51

我相信 Data::Dump::Streamer 可以序列化 coderefs。不过我自己没用过。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文