'Use of uninitialized value' 哈希上的消息,但我认为密钥存在

'Use of uninitialized value' message on hash but I think the key exists

我在调试时 运行 进入 st运行ge。顺便说一下,warnprint

foreach my $key (keys %{$changeIdsRelevant})
{
    warn($key); # Icf4e037 is printed once
}
warn((keys %{$changeIdsRelevant})[0]); # Icf4e037 is printed

我想知道键 Icf4e037 持有什么值,所以在第一个块之后打印了以下内容。如您所见,我不确定语法并打印了很多..

warn($changeIdsRelevant->{'Icf4e037'});
warn($changeIdsRelevant->{Icf4e037});
warn($changeIdsRelevant{Icf4e037}); # this one 'gave requires explicit package name' error

前 2 个,他们返回的不是值,而是

Use of uninitialized value in warn at <file_path>
Warning: something's wrong at <file_path>

这个if块也没有被执行

if (defined $changeIdsRelevant->{'Icf4e037'}) {
   warn('============================================================');
};

是说键Icf4e037存在但其值未定义?但我觉得世界上的每一个钥匙都可以这样说。有谁知道为什么它仍然返回 Icf4e037 keys <hash>?

哈希中可以存在键,但其值可以是undef

$h->{key} = undef;
warn $h->{key};  # Use of uninitialized value...

您可以使用 exists:

来检查是否存在而不考虑值
if (exists $h->{key}) {
    warn "key exists.";
    if (defined $h->{key}) {
        warn "Its value is defined.";
        if ($h->{key}) {
            warn "Its value is true.";
        }
    }
}