File Coverage

blib/lib/DBM/Any.pm
Criterion Covered Total %
statement 59 61 96.7
branch 4 6 66.6
condition 1 3 33.3
subroutine 15 15 100.0
pod 0 9 0.0
total 79 94 84.0


line stmt bran cond sub pod time code
1             package DBM::Any;
2              
3 2     2   61670 use strict;
  2         6  
  2         326  
4 2     2   16 use Carp;
  2         5  
  2         143  
5 2     2   14 use vars qw(@ISA $VERSION);
  2         5  
  2         168  
6              
7             $VERSION = '0.1';
8              
9             =head1 NAME
10              
11             DBM::Any - object-oriented interface to AnyDBM_File
12              
13             =head1 SYNOPSIS
14              
15             BEGIN {
16             @AnyDBM_File::ISA = qw(DB_File GDBM_File SDBM_File);
17             }
18             use DBM::Any;
19              
20             $db = new DBM::Any($filename, $flags, $mode[, optional...]);
21              
22             $val = $db->get($key);
23              
24             $db->put($key, $val);
25              
26             $db->delete($key);
27              
28             if ($db->exists($key)) { ... }
29              
30             for my $k ($db->keys()) { ... }
31              
32             for my $v ($db->values()) { ... }
33              
34             while (($k, $v) = $db->each()) { ... }
35              
36             $db->close();
37              
38             =head1 DESCRIPTION
39              
40             DBM::Any provides an object-oriented complement to AnyDBM_File's
41             tied interface. It was written because it didn't seem to exist on
42             CPAN, and the author likes BerkeleyDB's object-oriented interface,
43             but doesn't want to force people to get BerkeleyDB if they don't
44             want.
45              
46             The interface is a least common denominator among all available
47             database types; it contains the basic elements for keeping a
48             persistent hash on disk.
49              
50             The methods should map fairly well to regular operations on hashes.
51             Which is why I won't painstakingly document every method here; you
52             should already know how to deal with hashes.
53              
54             DBM::Any Objects should be considered opaque. Even if you know
55             what sort of database is underneath, you're a very naughty person
56             if you attempt to circumvent the prescribed intreface. :-)
57              
58             =cut
59              
60 2     2   13 use constant DBM_ANY_TIEREF => 0;
  2         4  
  2         171  
61 2     2   12 use constant DBM_ANY_HASHREF => 1;
  2         4  
  2         1661  
62              
63             sub new
64             {
65 5     5 0 844 my $proto = shift;
66 5   33     30 my $class = ref($proto)|| $proto;
67 5 100       20 if (@_ < 3) {
68 3         655 croak "Usage: ", __PACKAGE__, "->new(filename, flags, mode[, ...])";
69             }
70 2         6 my $filename = shift;
71 2         6 my $flags = shift;
72 2         4 my $mode = shift;
73 2         4 my @options = @_;
74 2         5 my $tieref;
75             my %tiehash;
76 2         273 $tieref = tie %tiehash, 'AnyDBM_File', $filename, $flags, $mode, @_;
77 2 50       31 return undef unless $tieref;
78 2         12 return bless [ $tieref, \%tiehash ], $class;
79             }
80              
81             sub put
82             {
83 5     5 0 244 my $self = shift;
84 5         7 my $key = shift;
85 5         6 my $value = shift;
86 5         173 $self->[DBM_ANY_HASHREF]->{$key} = $value;
87             }
88              
89             sub get
90             {
91 1     1 0 7 my $self = shift;
92 1         3 my $key = shift;
93 1         13 return $self->[DBM_ANY_HASHREF]->{$key};
94             }
95              
96             sub keys
97             {
98 1     1 0 1 my $self = shift;
99 1         2 return keys %{$self->[DBM_ANY_HASHREF]};
  1         44  
100             }
101              
102             sub values
103             {
104 1     1 0 452 my $self = shift;
105 1         3 return values %{$self->[DBM_ANY_HASHREF]};
  1         55  
106             }
107              
108             sub each
109             {
110 6     6 0 426 my $self = shift;
111 6         6 return each %{$self->[DBM_ANY_HASHREF]};
  6         87  
112             }
113              
114             sub exists
115             {
116 3     3 0 215 my $self = shift;
117 3         5 my $key = shift;
118 3         4 my $r;
119 3         3 eval { $r = exists $self->[DBM_ANY_HASHREF]->{$key}; };
  3         34  
120 3 50       12 if ($@) {
121 0         0 $r = $self->get($key);
122 0         0 return defined $r;
123             }
124 3         13 return $r;
125             }
126              
127             sub delete
128             {
129 1     1 0 33 my $self = shift;
130 1         3 my $key = shift;
131 1         32 return delete $self->[DBM_ANY_HASHREF]->{$key};
132             }
133              
134             sub close
135             {
136 4     4 0 53 my $self = shift;
137 4         9 eval { $self->[DBM_ANY_TIEREF]->sync(); }; ## Eh, worth a shot.
  4         77  
138 4         11 untie $self->[DBM_ANY_HASHREF];
139 4         338 undef $self->[DBM_ANY_TIEREF];
140             }
141              
142             sub DESTROY
143             {
144 2     2   210 $_[0]->close();
145             }
146              
147             =head1 BUGS
148              
149             Currently only supports DB_File access to Sleepycat's Berkeley DB.
150             I'd like to support BerkeleyDB.pm access as well. If there is an
151             elegant solution to this, I need more time to figure it out.
152              
153             The exists() method could be called on a database format which does
154             not support a simple existence check. For these I use a heuristic,
155             and attempt to retrieve the value associated with the key in
156             question. If the value is defined, then we say it exists. Because of this, I advise against explicit storage
157              
158             =head1 AUTHOR
159              
160             Tony Monroe Etmonroe+perl@nog.netE
161              
162             =head1 SEE ALSO
163              
164             L, L
165              
166             =cut
167              
168             1;
169             __END__