比方說,我有一個Perl腳本,它:如何在Perl
my $hash = {};
$hash->{'a'} = {aa => 'b'};
$hash->{'b'} = undef;
for (qw(a b c)) {
if(defined $hash->{$_}->{aa})
{
say "defined $_";
}
else
{
say "undef $_";
}
}
print Dumper $hash;
但我的輸出autocreates「C」,這是我不想要的。
defined a
undef b
undef c
$VAR1 = {
'c' => {},
'a' => {
'aa' => 'b'
},
'b' => {}
};
此外,我的發行版不允許我禁用autovivification。有沒有辦法讓一個子程序檢查每個級別?
Personnally,I' d返回對元素的引用,這樣,你不需要再次潛水來處理你發現的值定義。 '我的$ ref =潛水\%h,@k; if($ ref && defined $$ ref){say $$ ref; } else {say「[undef]」}' – ikegami