File Coverage

blib/lib/Tie/Hash/StructKeyed.pm
Criterion Covered Total %
statement 36 44 81.8
branch 6 12 50.0
condition 1 3 33.3
subroutine 10 13 76.9
pod n/a
total 53 72 73.6


line stmt bran cond sub pod time code
1             #!perl
2             package Tie::Hash::StructKeyed;
3             # $Id: StructKeyed.pm 344 2005-04-14 23:43:00Z hakim $
4              
5 3     3   78035 use strict; use warnings;
  3     3   6  
  3         109  
  3         19  
  3         6  
  3         102  
6 3     3   3406 use Tie::Hash;
  3         3249  
  3         93  
7 3     3   2617 use YAML;
  3         41250  
  3         1875  
8              
9             our $VERSION = "0.04";
10             our @ISA = qw (Tie::Hash);
11              
12             =head1 NAME
13              
14             Tie::Hash::StructKeyed - use structures like hashes and arrays as keys to a hash
15              
16             =head1 SYNOPSIS
17              
18             use Tie::Hash::StructKeyed;
19             tie %hash, 'Tie::Hash::StructKeyed';
20              
21             $hash{[1,2,3]} = 'Keyed by listref';
22              
23             my $h = { one=>1, two=>2 };
24             $hash{$h} = 'Keyed by hashref';
25            
26             =head1 DESCRIPTION
27              
28             Tie::Hash::StructKeyed ties a hash so that you can use arrays, hashes or
29             complex structures as the key of the hash.
30              
31             =head1 NOTE
32              
33             The current implementation uses YAML to generate the hash-key for the
34             structure. This is possibly the easiest way to get a powerful and flexible
35             key-hashing behaviour.
36              
37             It does mean that the behaviour for objects is undefined: Two objects with
38             the same representation will hash the same. The same object, after an internal
39             state change may hash differently. Behaviour of objects as keys (or as part
40             of a key) is subject to change in future versions.
41              
42             =cut
43              
44             sub TIEHASH {
45 2     2   352 my $something = shift;
46 2   33     19 my ($class) = ref ($something) || $something;
47 2         11 return bless {}, $class;
48             }
49              
50             sub STORE {
51 15     15   4483 my $self = shift;
52 15         34 my ($key,$value) = @_;
53              
54 15         50 my $yaml = Dump($key);
55 15         73795 $self->{$yaml}[0] = $key;
56 15         612 $self->{$yaml}[1] = $value;
57             }
58              
59             sub FETCH {
60 18     18   10204 my $self = shift;
61              
62 18 50       59 my $key = (@_ > 1) ? \@_ : shift;
63            
64 18         228 my $value = $self->{Dump($key)};
65 18 100       18345 return unless defined $value;
66 15         78 return $value->[1];
67             }
68              
69             sub DELETE {
70 0     0   0 my $self = shift;
71            
72 0 0       0 my $key = (@_ > 1) ? \@_ : shift;
73              
74 0         0 delete $self->{Dump($key)};
75             }
76              
77             sub CLEAR {
78 0     0   0 my $self = shift;
79              
80 0         0 %$self = ();
81             }
82              
83             sub EXISTS {
84 0     0   0 my $self = shift;
85              
86 0 0       0 my $key = (@_ > 1) ? \@_ : shift;
87 0         0 return exists $self->{Dump($key)};
88             }
89              
90             sub FIRSTKEY {
91 1     1   637 my $self = shift;
92            
93 1         5 my $a = keys %$self; # Resets the 'each' to the start
94 1         3 my $key = scalar each %$self;
95 1 50       10 return if (not defined $key);
96 1         7 return $self->{$key}[0];
97             }
98              
99             sub NEXTKEY {
100 9     9   12 my $self = shift;
101              
102 9         10 my ($last_key) = @_;
103 9         11 my $key = scalar each %$self;
104 9 100       27 return if (not defined $key);
105 8         28 return $self->{$key}[0];
106             }
107              
108             sub DESTROY {
109 2     2   1988 my $self = shift;
110             }
111              
112              
113             =head1 AUTHOR
114              
115             osfameron - osfameron@cpan.org
116              
117             =head1 VERSION
118              
119             Version 0.03 Apr 14 2005
120              
121             This program is free software; you can redistribute it
122             and/or modify it under the same terms as Perl itself.
123              
124             =head1 SEE ALSO
125              
126             perl perltie
127              
128             =cut
129              
130             1;