Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

I am trying to check if a hash key exists, for example:

use warnings;
use strict;
use feature qw(say);
use Data::Dump qw(dump);

my $h={a=>1,b=>2};

dump($h);

if (exists $h->{a}{b}) {
  say "Key exists.";
}
dump($h);

This gives:

{ a => 1, b => 2 }
Can't use string ("1") as a HASH ref while "strict refs" in use at ./p.pl line 12.

What is the reason for this error message?

like image 545
Håkon Hægland Avatar asked Oct 14 '14 06:10

Håkon Hægland


1 Answers

$h->{a}{b} implies that value of $h->{a} is hashref, and you want to check if key b for it exists.

Since $h->{a} is simple scalar (1) it can not be used as hashref (use strict prevents it), and thus message Can't use string (“1”) as a HASH ref

like image 82
mpapec Avatar answered Sep 30 '22 20:09

mpapec