Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store Hash of Hashes in 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});

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?

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?

Thanks in advance!!!

like image 958
keanni Avatar asked Dec 26 '22 20:12

keanni


1 Answers

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]]' );

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.

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.

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

And one other note, you could still use traits => [ 'Hash' ] and add some handles to give yourself some additional helpers, if you want.

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

like image 75
zostay Avatar answered Jan 01 '23 15:01

zostay