File Coverage

blib/lib/Tie/Hash/FixedKeys.pm
Criterion Covered Total %
statement 36 36 100.0
branch 4 4 100.0
condition n/a
subroutine 11 11 100.0
pod n/a
total 51 51 100.0


line stmt bran cond sub pod time code
1             # $Id: FixedKeys.pm 19 2008-06-30 14:51:47Z dave $
2              
3             =head1 NAME
4              
5             Tie::Hash::FixedKeys - Perl extension for hashes with fixed keys
6              
7             =head1 SYNOPSIS
8              
9             use Tie::Hash::FixedKeys;
10              
11             my @keys = qw(forename surname date_of_birth gender);
12             my %person;
13             tie %person, 'Tie;::Hash::FixedKeys', @keys;
14              
15             @person{@keys} = qw(Fred Bloggs 19700101 M);
16              
17             $person{height} = "6'"; # generates a warning
18              
19             or (new! improved!)
20              
21             use Tie::Hash::FixedKeys;
22              
23             my %person : FixedKeys(qw(forename surname date_of_birth gender));
24              
25             =head1 DESCRIPTION
26              
27             Tie::Hash::FixedKeys is a class which changes the behaviour of Perl hashes.
28             Any hash which is tied to this class can only contain a fixed set of keys.
29             This set of keys is given when the hash is tied. For example, after running
30             the code:
31              
32             my @keys = qw(forename surename date_of_birth gender);
33             my %person;
34             tie %person, 'Tie;::Hash::FixedKeys', @keys;
35              
36             the hash C<%person> can only contain the keys forename, surname,
37             date_of_birth and gender. Any attempt to set a value for another key
38             will generate a run-time warning.
39              
40             =head2 ATTRIBUTE INTERFACE
41              
42             From version 1.5, you can use attributes to set the keys for your hash.
43             You will need Attribute::Handlers version 0.76 or greater.
44              
45             =head2 CAVEAT
46              
47             The tied hash will always contain exactly one value for each of the keys
48             in the list. These values are initialised to C when the hash is
49             tied. If you try to C one if the keys, the effect is that the
50             value is reset to C.
51              
52             =head2 NOTE
53              
54             Versions of Perl from 5.8.0 include a module called L which
55             contains a function called C which does the same as this module
56             but in a faster and more powerful way. I recommend that you use that
57             method in place of this module.
58              
59             This module is left on CPAN as an example of tied hashes.
60              
61             =cut
62              
63             package Tie::Hash::FixedKeys;
64              
65 1     1   25354 use 5.006;
  1         4  
  1         39  
66 1     1   6 use strict;
  1         2  
  1         31  
67 1     1   6 use warnings;
  1         1  
  1         35  
68              
69 1     1   1077 use Tie::Hash;
  1         1034  
  1         25  
70 1     1   6 use Carp;
  1         2  
  1         57  
71 1     1   5 use vars qw(@ISA $VERSION);
  1         1  
  1         67  
72              
73 1     1   1034 use Attribute::Handlers autotie => { "__CALLER__::FixedKeys" => __PACKAGE__ };
  1         6220  
  1         9  
74              
75             @ISA = qw(Tie::StdHash);
76              
77             $VERSION = sprintf "%d", '$Revision: 19 $ ' =~ /(\d+)/;
78              
79             =head1 METHODS
80              
81             =head2 TIEHASH
82              
83             Creates a tied hash containing all the keys initialised to C.
84              
85             =cut
86              
87             sub TIEHASH {
88 1     1   2344 my $class = shift;
89              
90 1         2 my %hash;
91 1         8 @hash{@_} = (undef) x @_;
92              
93 1         8 bless \%hash, $class;
94             }
95              
96             =head2 STORE
97              
98             Attempts to store a value in the hash. If the key isn't in the valid
99             list (i.e. it doesn't already exist) the program croaks.
100              
101             =cut
102              
103             sub STORE {
104 3     3   1007 my ($self, $key, $val) = @_;
105              
106 3 100       17 unless (exists $self->{$key}) {
107 1         219 croak "invalid key [$key] in hash\n";
108             }
109 2         8 $self->{$key} = $val;
110             }
111              
112             =head2 DELETE
113              
114             Delete a value from the hash. Actually it just sets the value back to
115             C.
116              
117             =cut
118              
119             sub DELETE {
120 2     2   1816 my ($self, $key) = @_;
121              
122 2 100       11 return unless exists $self->{$key};
123              
124 1         3 my $ret = $self->{$key};
125              
126 1         2 $self->{$key} = undef;
127              
128 1         4 return $ret;
129             }
130              
131             =head2 CLEAR
132              
133             Clears all values but resetting them to C.
134              
135             =cut
136              
137             sub CLEAR {
138 1     1   989 my $self = shift;
139              
140 1         11 $self->{$_} = undef foreach keys %$self;
141             }
142              
143             1;
144             __END__