1. Perl
  2. Module
  3. here

Hash::Util - Prohibit the addition of hash keys

You can use the Hash::Util module to prevent the addition of hash keys.

use Hash::Util;
# limit
Hash::Util::lock_keys(%hash);

# lift
Hash::Util::unlock_keys(%hash);

You can use Hash::Util::lock_keys to limit the keys in a hash. If you specify only a hash as an argument, the addition of keys other than the current key is prohibited.

%hash = ();
# Limited to key1 and key2.
@restrict_keys = qw/key1 key2/;
Hash::Util::lock_keys(%hash, @restrict_keys);
# Remove restrictions.
Hash::Util::unlock_keys(%hash);

If you specify a key as the second argument, you can prohibit the addition of keys other than that key.

The Hash::Util::lock_keys function does not prohibit delete keys from being deleted. Only the addition of keys is prohibited.

Prohibit changing hash value and adding/deleting keys

Hash::Util::lock_hash (%hash);

# Lift the prohibition on hash changes
Hash::Util::lock_hash (%hash);

You can use the Hash::Util::lock_hash function to prevent key additions, deletions and value changes. You can treat the key and its corresponding value as read-only.

To make the hash completely read-only, use Hash::Util::lock_hash_recurse. If the hash value contains a reference, you can prevent the reference from changing the value it points to.

Example

This is an example that uses Hash::Util to limit the change of hash key and value.

use strict;
use warnings;

# Restrict changes to hash keys and values.
use Hash::Util;

my %hash;
my @restrict_keys;

# Limited to key1 and key2.
print "1-1: Prohibit the addition of hash keys.\n";
%hash = (key1 => 1, key2 => 2);
Hash::Util::lock_keys(%hash);

# Added key3.
# Die, so capture it with eval for an example.
eval {$hash{key3} = 3};
print "$@\n";

# Remove the prohibition on adding and deleting keys.
Hash::Util::unlock_keys(%hash);

# Limited to key1 and key2.
print "1-2: Prohibit the addition of keys other than the specified key.\n";
%hash = ();
@restrict_keys = qw/key1 key2/;

# Die when trying to add key3.
Hash::Util::lock_keys(%hash, @restrict_keys);
eval {$hash{key3} = 3};
print "$@\n";

# Remove the key restriction.
Hash::Util::unlock_keys(%hash);

print "2: Prohibit changing hash value and adding/deleting keys.\n";
%hash = (key1 => 1, key2 => 2);

# Die when trying to change the value.
Hash::Util::lock_hash (%hash);
eval {$hash{key1} = 3};
print "$@\n";

# Die when trying to add a key.
eval {$hash{key3} = 1};
print "$@\n";

# Lift the prohibition on hash changes
Hash::Util::lock_hash (%hash);

(Reference) eval

Output

1-1: Prohibit the addition of hash keys.
Attempt to access disallowed key'key3' in a restricted hash at - line 17.

1-2: Prohibit the addition of keys other than the specified key.
Attempt to access disallowed key'key3' in a restricted hash at - line 28.

2: Prohibit changing hash value and adding/deleting keys.
Modification of a read-only value attempted at - line 37.

Attempt to access disallowed key'key3' in a restricted hash at - line 40.

Related Informatrion