Tie::Hash::FixedKeys - Perl extension for hashes with fixed keys
use Tie::Hash::FixedKeys; my @keys = qw(forename surname date_of_birth gender); my %person; tie %person, 'Tie;::Hash::FixedKeys', @keys; @person{@keys} = qw(Fred Bloggs 19700101 M); $person{height} = "6'"; # generates a warning
or (new! improved!)
use Tie::Hash::FixedKeys; my %person : FixedKeys(qw(forename surname date_of_birth gender));
Tie::Hash::FixedKeys is a class which changes the behaviour of Perl hashes. Any hash which is tied to this class can only contain a fixed set of keys. This set of keys is given when the hash is tied. For example, after running the code:
my @keys = qw(forename surename date_of_birth gender); my %person; tie %person, 'Tie;::Hash::FixedKeys', @keys;
the hash %person can only contain the keys forename, surname, date_of_birth and gender. Any attempt to set a value for another key will generate a run-time warning.
%person
From version 1.5, you can use attributes to set the keys for your hash. You will need Attribute::Handlers version 0.76 or greater.
The tied hash will always contain exactly one value for each of the keys in the list. These values are initialised to undef when the hash is tied. If you try to delete one if the keys, the effect is that the value is reset to undef.
undef
delete
Dave Cross <dave@dave.org.uk>
perl(1), perltie(1).
To install Tie::Hash::FixedKeys, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Hash::FixedKeys
CPAN shell
perl -MCPAN -e shell install Tie::Hash::FixedKeys
For more information on module installation, please visit the detailed CPAN module installation guide.