justHelloWorld
justHelloWorld

Reputation: 6828

Accessing hash of hashes in Perl

I have this code:

my %options;

$options{"style"}{size} = "mini";
$options{"style"}{color} = "secondary";
diag("size=".$options{style}{size});
$self->applyStyle(%options);

Then:

sub applyStyle {
  my ($self, $options) = @_;
  diag("size=".$options->{style}{size});
}

But I get:

Can't use string ("style") as a HASH ref while "strict refs" in use at ...

How can I print (and use) the values?

Upvotes: 0

Views: 63

Answers (1)

Chris Turner
Chris Turner

Reputation: 8142

You're not passing in the hash correctly as the subroutine is expecting a hash ref and you're passing in a hash which will be expanded out into a list of key & values. Which is why it's trying to use "style" as a hashref as it is the first key it's being passed.

Just change the call to this and it should work.

$self->applyStyle(\%options);

Upvotes: 7

Related Questions