我想知道,在Moose中存储哈希哈希的最佳方法是什么。让我们举一个像这样的哈希:
my %hash = ('step1' => {'extraction' => \$object1,
'analysis' => \$object2},
'step2' => {'extraction' => \$object3,
'analysis' => \$object4});
但我想将这个保存在moose属性中。我该如何组织访问(阅读,写作)。网上的例子主要是“扁平”哈希。但是你可以使用像Moose :: Meta :: Attribute :: Native :: Trait :: Hash这样的助手。哈希散列有类似的东西吗?
原因是,我想迭代步骤键并访问其中的对象实例。或者有更好的,更像Moose的方式吗?
提前致谢!!!
答案 0 :(得分:10)
您可以在Moose对象中存储散列哈希,其方式与存储任何其他哈希的方式非常相似:
has steps => ( is => 'ro', isa => 'HashRef' );
但是,您可以更具体地将其声明为您需要存储的特定哈希类型,以验证存储在该槽中的任何内容是正确的类型:
has steps => ( is => 'ro', isa => 'HashRef[HashRef[Object]]' );
根据数据的不同,我也可能会将Object
更改为类名。你可以变得更加漂亮,并使用MooseX::Types和MooseX::Types::Structured来指定更严格的结构。
至于要跨过你的结构的助手,我不知道Moose或MooseX中的任何东西。如果你知道数据的结构,最好只实现一个子程序来自己做你需要的。您的代码可能会比任何通用遍历更好地执行您所需要的操作。
编辑/附加信息:每个Moose属性都会创建一个访问器方法,而不是您的类返回存储的值,因此访问数据是:
# 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' ]
并添加一些句柄给自己一些额外的助手。
如果数据结构比这更自由,您可能需要查看Data::Visitor之类的内容来迭代子例程中的结构。 (我有一些难以调试,Data :: Visitor的奇怪问题,所以我尽量避免使用它。)
答案 1 :(得分:0)
还有一种受Moose: How to get an array of objects? Traits?
启发的类型安全方法有一个类来保存具有traits => ['Hash']
的外部哈希(StepTool :: Steps)。这种方法可以使用例如无限深度嵌套。 Array
和Hash
es:
package StepTool;
use Moose;
has 'steps' => (
'is' => 'rw',
'isa' => 'StepTool::Steps',
'default' => sub { StepTool::Steps->new() },
);
package StepTool::Steps;
use Mouse;
has '_steps' => (
is => 'ro',
isa => 'HashRef[StepTool::Step]',
traits => ['Hash'],
default => sub { {} },
handles => {
# You'll probably want a fuller set here...
get => 'get',
set => 'set',
keys => 'keys',
}
);
package StepTool::Step;
use Mouse;
has 'extraction' => (
is => 'rw',
);
has 'analysis' => (
is => 'rw',
);
package main;
my $object1 = bless {}, 'Foobar1';
my $object2 = bless {}, 'Foobar2';
my $object3 = bless {}, 'Foobar3';
my $object4 = bless {}, 'Foobar4';
my $stepTool = StepTool->new();
# set up step1 one field at a time.
$stepTool->steps->set('step1', StepTool::Step->new());
# I have no idea why the OP wants references to objects
# everywhere but he does...
$stepTool->steps->get('step1')->extraction(\$object1);
$stepTool->steps->get('step1')->analysis(\$object2);
# set up step2 all at once
$stepTool->steps->set('step2', StepTool::Step->new(
extraction => \$object3,
analysis => \$object4
));
# or, less elegantly, initialize an entire StepTool:
my $stepTool2 = StepTool->new(
steps => StepTool::Steps->new(
_steps => {
step1 => StepTool::Step->new(
extraction => \$object1,
analysis => \$object2
),
step2 => StepTool::Step->new(
extraction => \$object3,
analysis => \$object4
),
}
),
);
printf "step1->analysis is a ref to an instance of class: %s\n",
ref(${$stepTool->steps->get('step1')->analysis});