如何在 Moose 中存储哈希的哈希? [英] How to store Hash of Hashes in Moose?
问题描述
我想知道,在 Moose 中存储哈希哈希的最佳方法是什么.让我们以这样的哈希为例:
i was wondering, what is the best way to store Hash of Hashes in Moose. Lets take for example a Hash like this:
my %hash = ('step1' => {'extraction' => \$object1,
'analysis' => \$object2},
'step2' => {'extraction' => \$object3,
'analysis' => \$object4});
但我想将这个保存在驼鹿属性中.我应该如何组织对此的访问(阅读、写作).网上的例子主要是扁平"哈希.但是你可以使用像 Moose::Meta::Attribute::Native::Trait::Hash 这样的助手.散列的散列是否有类似的东西?
but i want to save this one in a moose attribute. How should i organize the access (reading, writing) on this. Examples on the net are mostly for "flat" hashes. But then you can use helpers like Moose::Meta::Attribute::Native::Trait::Hash. Is there something similar for hash of hashes?
这样做的原因是,我想遍历步骤键并访问其中的对象实例.或者有没有更好、更像 Moose 的方法来做到这一点?
Reason for this is, that i want to iterate over the step-keys and access the object-instances in that. Or is there a better, more Moose-like way to do this?
提前致谢!!!
推荐答案
您可以在 Moose 对象中存储哈希的哈希,其方式与存储任何其他哈希的方式几乎相同:
You can store a hash of hashes in a Moose object in pretty much the same way as you would store any other hash:
has steps => ( is => 'ro', isa => 'HashRef' );
但是,您可以更具体地将其声明为您需要存储的特定类型的哈希,以验证存储在该插槽中的任何内容都是正确的:
You can, however, be more specific to declare it as the specific kind of hash you need to store as a way to verify that anything stored in that slot is the right kind of thing:
has steps => ( is => 'ro', isa => 'HashRef[HashRef[Object]]' );
根据数据,我也可能将此处的 Object
更改为类名.你可以更高级并使用 MooseX::Types 和 MooseX::Types::Structured 指定更严格的结构.
Depending on the data, I might also change Object
here to the class name. You can get even fancier and use MooseX::Types and MooseX::Types::Structured to specify an even more exacting structure.
至于帮助跨越你的结构,我不知道在 Moose 或 MooseX 中有什么可以做到这一点.如果您知道数据的结构,那么最好只实现一个子例程来完成您自己的需要.与任何通用遍历相比,您的代码可能会执行得更好,并能更好地完成您需要的操作.
As for helpers to to step over your structure, I don't know of anything in Moose or MooseX to do that. If you know the structure of your data, it's probably best to just implement a subroutine to do what you need yourself. Your code will likely perform better and do what you need better than any generic traversal.
编辑/附加信息:每个 Moose 属性都会创建一个访问器方法,而不是您的类,它返回存储的值,因此访问数据是:
Edit/Additional Info: Each Moose attribute creates an accessor method no your class which returns the stored value, so accessing the data is:
# Assuming we put the attribute in a package named StepTool
my $step_tool = StepTool->new(
steps => { 'step1' => {'extraction' => \$object1,
'analysis' => \$object2},
'step2' => {'extraction' => \$object3,
'analysis' => \$object4} },
);
# To do something one of the values
do_something($step_tool->steps->{step1}{extraction});
# To iterate over the structure, could be done in a method on StepTool
for my $step_name (keys %{ $step_tool->steps }) {
my $step = $step_tool->steps->{ $step_name };
for my $action_name (keys %$step) {
my $object = $step->{ $action_name };
do_something($object);
}
}
# If doing the above as a method, $self is the Moose object, so...
sub traverse_steps {
my ($self) = @_;
for my $step_name (keys %{ $self->steps }) {
... # just like above
}
}
另外一个注意事项,你仍然可以使用 traits =>[ 'Hash' ]
并添加一些句柄,如果需要,可以为自己提供一些额外的帮助程序.
And one other note, you could still use traits => [ 'Hash' ]
and add some handles to give yourself some additional helpers, if you want.
如果数据结构的形式更自由,您可能需要查看类似 Data 的内容::Visitor 在您的子程序中迭代您的结构.(我在使用 Data::Visitor 时遇到了一些难以调试的奇怪问题,所以我尽量避免它.)
If the data structure is more free form than that, you might want to look into something like Data::Visitor to iterate over your structure in your subroutine. (I have had some difficult to debug, weird problems with Data::Visitor, so I try to avoid it when I can.)
这篇关于如何在 Moose 中存储哈希的哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!