| 1 | package Basset::Container::Hash; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | #Basset::Container::Hash, copyright and (c) 2005, 2006 James A Thomason III | ||||||||||||||
| 4 | #Basset::Container::Hash is distributed under the terms of the Perl Artistic License. | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 10 | our $VERSION = '1.00'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | =pod | ||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 Basset::Container::Hash | ||||||||||||||
| 11 | |||||||||||||||
| 12 | Basset::Container::Hash implements a layered hash. The easiest way to explain is with an example: | ||||||||||||||
| 13 | |||||||||||||||
| 14 | my %x = ('a' => 'b'); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | tie my %y, 'Basset::Container::Hash', \%x; #<- %x is the parent of 'y'. | ||||||||||||||
| 17 | |||||||||||||||
| 18 | print $x{'a'}; #prints b | ||||||||||||||
| 19 | print $y{'a'}; #prints b (inherited from x) | ||||||||||||||
| 20 | $y{'a'} = 'foo'; | ||||||||||||||
| 21 | $y{'z'} = 'bar'; | ||||||||||||||
| 22 | print $x{'a'}; #prints b | ||||||||||||||
| 23 | print $y{'a'}; #prints foo (overriden in y) | ||||||||||||||
| 24 | print $x{'z'}; #prints undef (not defined in x | ||||||||||||||
| 25 | print $y{'z'}; #prints bar (overridden from x) | ||||||||||||||
| 26 | delete $y{'a'}; | ||||||||||||||
| 27 | print $x{'a'}; #prints b | ||||||||||||||
| 28 | print $y{'a'}; #prints b (inherited from x) | ||||||||||||||
| 29 | $x{'b'} = 'c'; | ||||||||||||||
| 30 | print $x{'b'}; #prints c | ||||||||||||||
| 31 | print $y{'b'}; #prints c (inherited from x) | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =cut | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 10 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 10 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | # we're going to use an array underneath and bypass Basset::Object & accessor, for speed reasons. | ||||||||||||||
| 39 | # since we only talk to it via the tie interface, we can get away with it. | ||||||||||||||
| 40 | 10 | our $internal_hash = 0; | |||||||||||||
| 41 | 10 | our $parent = 1; | |||||||||||||
| 42 | 10 | our $gotoparent = 2; | |||||||||||||
| 43 | |||||||||||||||
| 44 | 119 | ▶ | sub TIEHASH { | ||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | 119 | my $class = shift; | |||||||||||||
| 47 | 119 | my $parent = shift; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 119 | return bless [ | |||||||||||||
| 50 | {}, | ||||||||||||||
| 51 | $parent, | ||||||||||||||
| 52 | 0 | ||||||||||||||
| 53 | ], $class; | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 544 | ▶ | sub STORE { | ||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||
| 57 | 544 | my $self = shift; | |||||||||||||
| 58 | 544 | my $key = shift; | |||||||||||||
| 59 | 544 | my $value = shift; | |||||||||||||
| 60 | |||||||||||||||
| 61 | 544 | $self->[$internal_hash]->{$key} = $value; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 724 | ▶ | sub FETCH { | ||||||||||||
|
Subroutine
FETCH: called CC=3 SCAR=11.2
| |||||||||||||||
| 65 | 724 | my $self = shift; | |||||||||||||
| 66 | 724 | my $key = shift; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 724 | my $internal = $self->[$internal_hash]; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 724 | ▶ | if (exists $internal->{$key}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 71 | 40 | return $internal->{$key}; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | elsif (my $parent = $self->[$parent]) { | ||||||||||||||
| 74 | 684 | return $parent->{$key}; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | else { | ||||||||||||||
| 77 | 0 | return; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 3 | ▶ | sub EXISTS { | ||||||||||||
|
Subroutine
EXISTS: called CC=3 SCAR=11.2
| |||||||||||||||
| 82 | 3 | my $self = shift; | |||||||||||||
| 83 | 3 | my $key = shift; | |||||||||||||
| 84 | |||||||||||||||
| 85 | 3 | my $internal = $self->[$internal_hash]; | |||||||||||||
| 86 | |||||||||||||||
| 87 | 3 | ▶ | if (exists $internal->{$key}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 88 | 1 | return exists $internal->{$key}; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | elsif (my $parent = $self->[$parent]) { | ||||||||||||||
| 91 | 2 | return exists $parent->{$key}; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | else { | ||||||||||||||
| 94 | 0 | return; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 2 | ▶ | sub DELETE { | ||||||||||||
|
Subroutine
DELETE: called CC=1 SCAR=0.0
| |||||||||||||||
| 99 | 2 | my $self = shift; | |||||||||||||
| 100 | 2 | my $key = shift; | |||||||||||||
| 101 | |||||||||||||||
| 102 | 2 | delete $self->[$internal_hash]->{$key}; | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 1 | ▶ | sub CLEAR { | ||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||
| 106 | 1 | shift->[$internal_hash] = {}; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 13 | ▶ | sub FIRSTKEY { | ||||||||||||
|
Subroutine
FIRSTKEY: called CC=3 SCAR=11.0
| |||||||||||||||
| 110 | 13 | my $self = shift; | |||||||||||||
| 111 | |||||||||||||||
| 112 | 13 | $self->[$gotoparent] = 0; | |||||||||||||
| 113 | |||||||||||||||
| 114 | 13 | my $internal = $self->[$internal_hash]; | |||||||||||||
| 115 | 13 | my $c = keys %$internal; | |||||||||||||
| 116 | |||||||||||||||
| 117 | 13 | my ($k, $v) = each %$internal; | |||||||||||||
| 118 | |||||||||||||||
| 119 | 13 | ▶ | unless (defined $k) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 2 | ▶ | if (my $parent = $self->[$parent]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 2 | $self->[$gotoparent] = 1; | |||||||||||||
| 122 | 2 | ($k, $v) = each %$parent; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 13 | return $k; | |||||||||||||
| 127 | |||||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 131 | ▶ | sub NEXTKEY { | ||||||||||||
|
Subroutine
NEXTKEY: called CC=6 SCAR=17.9
| |||||||||||||||
| 131 | 131 | my $self = shift; | |||||||||||||
| 132 | |||||||||||||||
| 133 | 131 | my $internal = $self->[$internal_hash]; | |||||||||||||
| 134 | |||||||||||||||
| 135 | 131 | ▶ | unless ($self->[$gotoparent]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 34 | my ($k, $v) = each %$internal; | |||||||||||||
| 137 | 34 | ▶ | if (defined $k) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 138 | 23 | return $k; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 108 | ▶ | if (my $parent = $self->[$parent]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | 108 | $self->[$gotoparent] = 1; | |||||||||||||
| 144 | 108 | ▶ | while (my ($k, $v) = each %$parent) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | 110 | ▶ | return $k unless exists $internal->{$k}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | } | ||||||||||||||
| 147 | 12 | $self->[$gotoparent] = 0; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | 12 | return; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 1 | ▶ | sub SCALAR { | ||||||||||||
|
Subroutine
SCALAR: called CC=1 SCAR=0.0
| |||||||||||||||
| 154 | 1 | my $self = shift; | |||||||||||||
| 155 | |||||||||||||||
| 156 | 1 | my $internal = $self->[$internal_hash]; | |||||||||||||
| 157 | |||||||||||||||
| 158 | 1 | my %flat = (); | |||||||||||||
| 159 | 1 | @flat{keys %$internal} = values %$internal; | |||||||||||||
| 160 | |||||||||||||||
| 161 | 1 | return scalar %flat; | |||||||||||||
| 162 | |||||||||||||||
| 163 | } | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 10 | 1; | |||||||||||||
| 166 | |||||||||||||||
| 167 | =pod | ||||||||||||||
| 168 | |||||||||||||||
| 169 | =begin btest(Basset::Container::Hash) | ||||||||||||||
| 170 | |||||||||||||||
| 171 | my %x = ('a' => 'b'); | ||||||||||||||
| 172 | |||||||||||||||
| 173 | tie my %y, 'Basset::Container::Hash', \%x; #<- %x is the parent of 'y'. | ||||||||||||||
| 174 | |||||||||||||||
| 175 | $y{'a'} = 'c'; | ||||||||||||||
| 176 | $y{'b'} = 'd'; | ||||||||||||||
| 177 | |||||||||||||||
| 178 | $test->is($x{'a'}, 'b', '$x{a} = b'); | ||||||||||||||
| 179 | $test->is($y{'a'}, 'c', '$y{a} = c'); | ||||||||||||||
| 180 | $test->is($y{'b'}, 'd', '$y{b} = d'); | ||||||||||||||
| 181 | $test->is($x{'b'}, undef, '$x{b} is undef'); | ||||||||||||||
| 182 | $test->is(scalar(%y), '2/8', 'scalar %y works'); | ||||||||||||||
| 183 | delete $y{'a'}; | ||||||||||||||
| 184 | $test->is($y{'a'}, 'b', '$y{a} is now b'); | ||||||||||||||
| 185 | $test->ok(exists $y{'a'} != 0, '$y{a} exists'); | ||||||||||||||
| 186 | $test->ok(exists $y{'b'} != 0, '$y{b} exists'); | ||||||||||||||
| 187 | $test->ok(exists $y{'c'} == 0, '$y{c} does not exist'); | ||||||||||||||
| 188 | delete $y{'b'}; | ||||||||||||||
| 189 | |||||||||||||||
| 190 | my ($key, $value) = each %y; | ||||||||||||||
| 191 | |||||||||||||||
| 192 | $test->is($key, 'a', 'only key left is a'); | ||||||||||||||
| 193 | |||||||||||||||
| 194 | $y{'new'} = 'value'; | ||||||||||||||
| 195 | |||||||||||||||
| 196 | my ($key2, $value2) = (keys %y)[0]; | ||||||||||||||
| 197 | |||||||||||||||
| 198 | $test->is($key2, 'new', 'first set key is new'); | ||||||||||||||
| 199 | |||||||||||||||
| 200 | my @keys = sort keys %y; | ||||||||||||||
| 201 | $test->is($keys[0], 'a', 'first key is a'); | ||||||||||||||
| 202 | $test->is($keys[1], 'new', 'second key is new'); | ||||||||||||||
| 203 | |||||||||||||||
| 204 | %y = (); | ||||||||||||||
| 205 | my @keys2 = sort keys %y; | ||||||||||||||
| 206 | $test->is(scalar @keys2, 1, 'only one key remains'); | ||||||||||||||
| 207 | |||||||||||||||
| 208 | =end btest(Basset::Container::Hash) | ||||||||||||||
| 209 | |||||||||||||||
| 210 | =cut | ||||||||||||||