Name

Tie::Subset::Hash - Tie a hash to a subset of another hash

Synopsis

use Tie::Subset::Hash;
my %hash = ( foo=>11, bar=>22, quz=>33 );
tie my %subset, 'Tie::Subset::Hash', \%hash, ['bar','quz'];
print "$subset{bar}\n";  # prints "22"
$subset{quz}++;          # modifies $hash{quz}

Description

This class for tied hashes provides a "view" of a subset of a hash.

tieing
tie my %subset, 'Tie::Subset::Hash', \%hash, \@keys;

You must specify which subset of keys from the original hash can be accessed via the tied hash. (Keys that do not yet exist in the original hash may be specified.)

Fetching

If the key is in the subset, the value from the underlying hash is returned, otherwise returns nothing (undef).

Storing

If the key is in the subset, the new value will be stored in the underlying hash, otherwise the operation is ignored and a warning issued.

exists

Will return true only if the key is in the subset and it exists in the underlying hash.

Iterating (each, keys, etc.)

Only keys that exist both in the subset and the underlying hash are iterated over. The iterator of the underlying hash is utilized, so iterating over the tied hash will affect the state of the iterator of the underlying hash.

deleteing

If the key is in the subset, the key will be deleted from the underlying hash, but not the subset. Otherwise, the operation is ignored and a warning issued.

Clearing

Not (yet) supported (because it is ambiguous whether this operation should delete keys from the underlying hash or not). Attempting to clear the tied hash currently does nothing and causes a warning to be issued.

A future version of this module may lift this limitation (if a useful default behavior exists).

See Also

Tie::Subset::Hash::Masked

"See Also" in Tie::Subset

Author, Copyright, and License

Copyright (c) 2018-2023 Hauke Daempfling (haukex@zero-g.net).

This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.

For more information see the Perl Artistic License, which should have been distributed with your copy of Perl. Try the command perldoc perlartistic or see http://perldoc.perl.org/perlartistic.html.