File Coverage

blib/lib/Tie/Hash/Abbrev.pm
Criterion Covered Total %
statement 24 24 100.0
branch 5 8 62.5
condition 4 6 66.6
subroutine 6 6 100.0
pod 1 3 33.3
total 40 47 85.1


line stmt bran cond sub pod time code
1             package Tie::Hash::Abbrev;
2              
3             =head1 NAME
4              
5             Tie::Hash::Abbrev - a hash which can be accessed using abbreviated keys
6              
7             =head1 SYNOPSIS
8              
9             use Tie::Hash::Abbrev;
10              
11             tie my %hash, 'Tie::Hash::Abbrev';
12              
13             %hash = ( sonntag =>0, montag =>1, dienstag=>2, mittwoch =>3,
14             donnerstag=>4, freitag=>5, samstag =>6,
15             sunday =>0, monday =>1, tuesday =>2, wednesday=>3,
16             thursday =>4, friday =>5, saturday=>6 );
17              
18             print $hash{do}; # will print "4"
19             print $hash{fr}; # undef
20             print $hash{t}; # undef
21              
22             my @deleted = tied(%hash)->delete_abbrev( qw{do fr t} );
23             # will delete element "donnerstag"; @deleted will be (4)
24              
25             =head1 DESCRIPTION
26              
27             This module implements a subclass of L.
28             The contents of hashes tied to this class may be accessed via unambiguously
29             abbreviated keys.
30             (Please note, however, that this is not true for
31             L hash elements;
32             for that, can use L via the object interface.)
33              
34             While you could achieve a similar behaviour by using the standard module
35             L for mapping abbreviations to the original keys, the (main)
36             advantage of Tie::Hash::Abbrev is that you do not have to calculate all possible
37             abbreviations in advance each time a key is altered, and you do not have to
38             store them in memory.
39              
40             =cut
41              
42 2     2   22178 use strict;
  2         3  
  2         79  
43 2     2   17 use vars '$VERSION';
  2         5  
  2         91  
44 2     2   10 use base 'Tie::Hash::Array';
  2         4  
  2         1229  
45              
46             $VERSION = 0.10;
47              
48             =head1 ADDITIONAL METHODS
49              
50             =head2 delete_abbrev
51              
52             my @deleted = tied(%hash)->delete_abbrev('foo','bar');
53              
54             Will delete all elements on the basis of all unambiguous (in the sense of this
55             module or the subclass used) abbreviations given as arguments and return a
56             (possibly empty) list of all deleted values.
57              
58             =cut
59              
60             sub delete_abbrev {
61 2     2 1 1720 my $self = shift;
62 2         4 my @deleted;
63 2         6 for (@_) {
64             next
65             unless
66 23 50       70 defined( my $pos1 = $self->valid( $_, my $pos = $self->pos($_) ) );
67 23         33 my $i = 0;
68 23         82 push @deleted, grep $i++ & 1, $self->splice( $pos, 2 + $pos1 - $pos );
69             }
70             @deleted
71 2         40 }
72              
73 28     28 0 196 sub equals { '' }
74              
75             sub valid {
76 111     111 0 187 my ( $self, $key, $pos ) = @_;
77             return undef
78 111 50 33     598 unless $pos <= $#$self && $key eq substr $self->[$pos], 0, length $key;
79 111         199 my $value = $self->[ $pos + 1 ];
80 111 50       254 return $value if $self->[$pos] eq $key; # always match if exact key is given
81 111   100     568 while ( $pos + 2 <= $#$self && $key eq substr $self->[ $pos + 2 ], 0,
82             length $key )
83             {
84             return undef
85 77 100       240 unless $self->equals( $value, $self->[ ( $pos += 2 ) + 1 ] );
86             }
87 69         249 $pos;
88             }
89              
90             =head1 SUBCLASSING
91              
92             Please do not rely on the implementation details of this class for now,
93             since they may still be subject to change.
94              
95             If you'd like to subclass this module, please let me know;
96             perhaps we can agree on some standards then.
97              
98             =head1 BUGS
99              
100             None known so far.
101              
102             =head1 AUTHOR
103              
104             Martin H. Sluka
105             mailto:perl@sluka.de
106             http://martin.sluka.de/
107              
108             =head1 COPYRIGHT & LICENCE
109              
110             This program is free software; you can redistribute
111             it and/or modify it under the same terms as Perl itself.
112              
113             The full text of the license can be found in the
114             LICENSE file included with this module.
115              
116             =head1 SEE ALSO
117              
118             L, L
119              
120             =cut
121              
122             1