perlmojoliciousmojolicious-lite

Errors accessing hash in mojolicious


I am having issues accessing my hash in mojolicious.

my %managers = (
'IT' => {
    'name' => 'Mike',
    'id'   => 1,
    'num_of_employees' => 15,
},
'Sales' => {
    'name' => 'John',
    'id' => 33,
    'num_of_employees'=> 50,
},
);

In perl I can access the data like $managers{'IT'}{'name'} would print out Mike. How would I do the same in mojolicious?

Being passed to my template
$g->stash(manage => \%managers);
<%== $manage{'IT'}{'name'} %>

The above throws an error. printing <%== $manage %> gives a HASH(0x1335430) location.


Solution

  • In your template $manage is a hash ref not a hash, so you need to dereference it by using the -> operator like this

    <%== $manager->{'IT'}{'name'} %>