我应该如何序列化 Moose 对象数组?
我使用 MooseX::Storage
来序列化 < a href="http://search.cpan.org/perldoc?Moose" rel="nofollow">Moose 对象。我可以使用它将多个 Moose 对象序列化到同一个文件,或更具体地说,序列化 Moose 对象的数组或哈希吗?
我想我可以定义另一个 Moose 对象('array_of_myobj'),但这不是很优雅。
那么,您建议如何序列化 Moose 对象的数组(或哈希)?
I use MooseX::Storage
for serialization of Moose objects. Can I use it for serialization of multiple Moose objects to the same file, or more specifically, an array or a hash of Moose objects?
I guess I can define another Moose objects ('array_of_myobj') but this isn't very elegant.
So, how would you recommend to serialize an array (or a hash) of Moose objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必仅仅因为 MooseX::Storage 可用就让 MooseX::Storage 管理您的文件 IO。您可以使用它将对象打包到 hashrefs 中,然后使用 YAML 或 Storable 或您自己的任何内容转储它们。这就是 MooseX::Storage 区分三个序列化级别的原因。
也就是说,我不同意创建一个包含要在给定上下文中序列化的所有其他对象的单个顶级对象是不优雅的。
You don't have to let MooseX::Storage manage your file IO just because it's available. You could use it to pack your objects into hashrefs and then dump them with YAML or Storable or whatever yourself. This is why MooseX::Storage distinguishes between three levels of serialization.
That said, I disagree that it is inelegant to make a single top-level object that contains all the other objects you want to serialize in a given context.