在使用“严格参考”时,不能使用字符串(“1”)作为HASH参考

时间:2014-10-14 06:26:27

标签: perl

我正在尝试检查是否存在哈希键,例如:

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);

这给出了:

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

此错误消息的原因是什么?

1 个答案:

答案 0 :(得分:9)

$h->{a}{b}表示$h->{a}的值为hashref,您想检查其中是否存在密钥b

由于$h->{a}是简单标量(1),因此无法用作hashref(use strict阻止它),因此消息Can't use string (“1”) as a HASH ref