flymike
flymike

Reputation: 945

How to access Perl hash defined as a constant

If I define a Perl hash as a constant, like:

use constant SITES => {foo => 1, bar => 2};

how do I retrieve the values for foo and bar? $SITES{foo} does not work.

Upvotes: 2

Views: 583

Answers (2)

Jassi
Jassi

Reputation: 541

As it is a constant so no need to use $ infront of the variable and value is hash reference not just hash so HASH{'key'} would not work. use instead SITE->{'foo'}

Upvotes: 0

Sean
Sean

Reputation: 29772

my $site_foo = SITES->{foo};
my $site_bar = SITES->{bar};

SITES is essentially a subroutine that returns a hash reference.

Upvotes: 7

Related Questions