Cannot use string ("1") as a HASH ref when using "strong links"

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?

+5
source share
1 answer

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

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

+8
source

Source: https://habr.com/ru/post/1204618/


All Articles