The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


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


 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}


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

 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.)


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


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.


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 are both in the subset and the underlying hash are iterated over.


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.


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

"See Also" in Tie::Subset

Author, Copyright, and License

Copyright (c) 2018 Hauke Daempfling (

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