哪些推荐的 Perl 模块可以序列化 Moose 对象?
我通常将 Storable
与 nstore
一起使用,但现在我有一个 模块有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设置
$Data 后,您可以使用 Data::Dumper 转储 coderef ::Dumper::Deparse
为真值,但这仅用于调试目的,不适用于序列化。我建议您回过头来看看为什么 MooseX::Storage 不适合您,因为作者非常努力地为 Moose 对象序列化提供一个充分抽象且强大的解决方案。
更新:看起来您在序列化
_offset_sub
属性时遇到了问题,如 这个问题。由于该属性有一个构建器,并且它的构造相当简单(它只是查看另一个属性的当前值),因此您根本不需要序列化它——当您反序列化对象并想要再次使用它时,第一次调用$this->offset
时将调用构建器。因此,您应该能够将其标记为“不序列化”:最后,这有点正交,但您可以折叠
offset
并使用原生属性“Code”特征将
_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":Lastly, this is somewhat orthogonal, but you can fold the
offset
and_offset_sub
attributes together by using the native attribute 'Code' trait:看看
KiokuDB
,它是用Moose
所以它应该真正覆盖所有角落(注意。我自己没有尝试过,但我保留意思是!)/I3az/
Have a look at
KiokuDB
, its designed with and forMoose
so it should really cover all the corners (NB. I haven't tried it myself but I keep meaning to!)/I3az/
我相信 Data::Dump::Streamer 可以序列化 coderefs。不过我自己没用过。
I believe Data::Dump::Streamer can serialize coderefs. Haven't used it myself though.