0

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.

user3525290
  • 1,557
  • 2
  • 20
  • 47
  • " $g->stash(manage => \%managers); <%== $manager{'IT'}{'name'} %> " shouldn't you call both the same? either "manage" or "manager"? your key in perl is "manage", but in your template you try to access "manager" – Chris Mar 05 '19 at 12:47
  • sorry was a typo. but still getting an error when i print `<%== $manage{'IT'}{'name'} %>` error is ` Global symbol "%manage" requires explicit package name (did you forget to declare "my %manage"?)` and `<%== $manage %> gives a HASH(0x1335430)` is giving a location – user3525290 Mar 05 '19 at 12:55

1 Answers1

4

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'} %>
JGNI
  • 3,933
  • 11
  • 21
  • I assumed that was an issue after more reading. I am now getting `Can't use string ("Sales") as a HASH ref while "strict refs"`. – user3525290 Mar 05 '19 at 13:26
  • That error means that `$manager->{'IT'}` is set to the string `Sales` and not to another hash reference like you're expecting. – mob Mar 05 '19 at 13:54
  • 1
    Not sure but now it's working. Maybe a typo. Thanks everyone. – user3525290 Mar 05 '19 at 14:26