How can I check if a key exists in a deep Perl hash?

后端 未结 5 944
渐次进展
渐次进展 2020-12-08 14:43

If I understand correctly, calling if (exists $ref->{A}->{B}->{$key}) { ... } will spring into existence $ref->{A} and $ref->

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 15:16

    Take a look at Data::Diver. E.g.:

    use Data::Diver qw(Dive);
    
    my $ref = { A => { foo => "bar" } };
    my $value1 = Dive($ref, qw(A B), $key);
    my $value2 = Dive($ref, qw(A foo));
    

提交回复
热议问题