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

NAME

Tie::Hash::FixedKeys - Perl extension for hashes with fixed keys

SYNOPSIS

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

DESCRIPTION

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.

ATTRIBUTE INTERFACE

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.

CAVEAT

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.

NOTE

Versions of Perl from 5.8.0 include a module called Hash::Util which contains a function called lock_keys which does the same as this module but in a faster and more powerful way. I recommend that you use that method in place of this module.

This module is left on CPAN as an example of tied hashes.

METHODS

TIEHASH

Creates a tied hash containing all the keys initialised to undef.

STORE

Attempts to store a value in the hash. If the key isn't in the valid list (i.e. it doesn't already exist) the program croaks.

DELETE

Delete a value from the hash. Actually it just sets the value back to undef.

CLEAR

Clears all values but resetting them to undef.

AUTHOR

Dave Cross <dave@mag-sol.com>

COPYRIGHT AND LICENSE

Copyright (C) 2001, Magnum Solutions Ltd. All Rights Reserved.

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

SEE ALSO

perl(1), perltie(1).