our @EXPORT_OK = qw(lock_keys unlock_keys lock_value unlock_value
Hash::Util - A selection of general-utility hash subroutines
use Hash::Util qw(lock_keys unlock_keys
%hash = (foo => 42, bar => 23);
lock_keys(%hash, @keyset);
lock_value (%hash, 'foo');
unlock_value(%hash, 'foo');
C<Hash::Util> contains special functions for manipulating hashes that
don't really warrant a keyword.
By default C<Hash::Util> does not export anything.
5.8.0 introduces the ability to restrict a hash to a certain set of
keys. No keys outside of this set can be added. It also introduces
the ability to lock an individual key so it cannot be deleted and the
This is intended to largely replace the deprecated pseudo-hashes.
Restricts the given %hash's set of keys to @keys. If @keys is not
given it restricts it to its current keyset. No more keys can be
added. delete() and exists() will still work, but it does not effect
Removes the restriction on the %hash's keyset.
Internals
::hv_clear_placeholders
%$hash;
my %keys = map { ($_ => 1) } @keys;
my %original_keys = map { ($_ => 1) } keys %$hash;
foreach my $k (keys %original_keys) {
die sprintf "Hash has key '$k' which is not in the new key ".
"set at %s line %d\n", (caller)[1,2]
$hash->{$k} = undef unless exists $hash->{$k};
Internals
::SvREADONLY
%$hash, 1;
delete $hash->{$k} unless $original_keys{$k};
Internals
::SvREADONLY
%$hash, 1;
Internals
::SvREADONLY
%$hash, 0;
Locks and unlocks an individual key of a hash. The value of a locked
%hash must have already been locked for this to have useful effect.
carp
"Cannot usefully lock values in an unlocked hash"
unless Internals
::SvREADONLY
%$hash;
Internals
::SvREADONLY
$hash->{$key}, 1;
Internals
::SvREADONLY
$hash->{$key}, 0;
lock_hash() locks an entire hash, making all keys and values readonly.
No value can be changed, no keys can be added or deleted.
unlock_hash() does the opposite of lock_hash(). All keys and values
are made read/write. All values can be changed and keys can be added
foreach my $key (keys %$hash) {
lock_value
(%$hash, $key);
foreach my $key (keys %$hash) {
unlock_value
(%$hash, $key);
Michael G Schwern <schwern@pobox.com> on top of code by Nick
Ing-Simmons and Jeffrey Friedl.
L<Scalar::Util>, L<List::Util>, L<Hash::Util>