make perl shout when trying to access undefined ha

2019-04-23 18:39发布

I think the title is self-explanatory. Many times I have small typos and I get unexpected results when trying to access undefined hash keys. I know I can add some defined check before each time I access a hash key, but I wonder if there's any cleaner way to warn against such cases....

Best, Dave

标签: perl hash
3条回答
趁早两清
2楼-- · 2019-04-23 18:52

You can write a simple function for this:

sub get {
    my ($hash, $key) = @_;
    die "No such key: $key" unless exists $hash->{$key};
    return $hash->{$key};
}

my %hash = (...);    
my $val = get(\%hash, "mykey");
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-23 18:56

Use Hash::Util:

use Hash::Util "lock_keys";
my %hash = (foo => 42, bar => 23);
lock_keys(%hash);
print $hash{foo};
print $hash{baz};
print $hash{bar};

output:

42
Attempt to access disallowed key 'baz' in a restricted hash at foo line 5.

There are other functions that allow specifying which keys to allow, not just defaulting to what's already there.

查看更多
我命由我不由天
4楼-- · 2019-04-23 18:57

This is probably best done with a tied hash. Tied variables allow you to define the implementation of the low level operations of the variable. In this case, we want a special fetch method that dies when accessing non-existant keys:

use warnings;
use strict;

{package Safe::Hash;
        require Tie::Hash;
        our @ISA = 'Tie::StdHash';
        use Carp;

        sub FETCH { 
                exists $_[0]{$_[1]} or croak "no key $_[1]";
                $_[0]{$_[1]}
        }
}

tie my %safe => 'Safe::Hash';

$safe{a} = 5;  # ok

print $safe{a}, "\n";  # ok

$safe{b} = 10; # ok 

print $safe{bb}, "\n";  # dies

In the implementation of Safe::Hash above, I first load Tie::Hash which provides Tie::StdHash. Setting @ISA to Tie::StdHash provides our new package with tie methods that behave the same way as normal hashes. Each of the tie methods are outlined on http://perldoc.perl.org/perltie.html

In this case the only method to override is FETCH which is passed a reference to the hidden tied object (a hashref in this case), and the key to use. It checks if the slot exists, and either returns it or throws an error

查看更多
登录 后发表回答