The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

Tie::Subset::Hash::Masked - Tie a hash to mask some of its keys

Synopsis

 use Tie::Subset::Hash::Masked;
 use Data::Dumper;
 my %hash = ( foo=>11, bar=>22, quz=>33 );
 tie my %masked, 'Tie::Subset::Hash::Masked', \%hash, ['bar','quz'];
 print Dumper(\%masked);  # shows only { foo => 11 }
 $masked{baz}++;          # adds this key to %masked and %hash

Description

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

tieing
 tie my %masked, 'Tie::Subset::Hash::Masked', \%hash, \@mask;

You must specify which keys from the original hash should be masked in the tied hash. (Keys that do not yet exist in the original hash may also be specified.)

Fetching

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

Storing

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

exists

Will return true only if the key exists in the underlying hash and the key is not masked.

Iterating (each, keys, etc.)

Only keys that exist in the underlying hash and that aren't masked 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 masked, the operation is ignored and a warning issued, otherwise, the key will be deleted from the underlying hash.

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

"See Also" in Tie::Subset

Author, Copyright, and License

Copyright (c) 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.