blib/lib/DBM/Any.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 66.6% 4 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 9 totaltotal 82.6% 81 / 98 CC 5 SCAR 5.6
CC
5
Cov
92%
new
4.1
exists
2.1
BEGIN
1.0
CRAP 1.8
↑ Summary
1 package DBM::Any;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 $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 ▶use constant DBM_ANY_TIEREF => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use constant DBM_ANY_HASHREF => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 sub new
64 5 ▶{
Subroutine new: called CC=4 SCAR=14.0
Pod undocumented
65 5 my $proto = shift;
66 5 ▶ my $class = ref($proto)|| $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
67 5 ▶ if (@_ < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 3 2 5
68 3 croak "Usage: ", __PACKAGE__, "->new(filename, flags, mode[, ...])";
69 }
70 2 my $filename = shift;
71 2 my $flags = shift;
72 2 my $mode = shift;
73 2 my @options = @_;
74 2 my $tieref;
75 my %tiehash;
76 2 $tieref = tie %tiehash, 'AnyDBM_File', $filename, $flags, $mode, @_;
77 2 ▶ return undef unless $tieref;
Branch: 1 branch
BranchTrueFalseTotal
unless $tieref 0 2 2
78 2 return bless [ $tieref, \%tiehash ], $class;
79 }
80  
81 sub put
82 5 ▶{
Subroutine put: called CC=1 SCAR=0.0
Pod undocumented
83 5 my $self = shift;
84 5 my $key = shift;
85 5 my $value = shift;
86 5 $self->[DBM_ANY_HASHREF]->{$key} = $value;
87 }
88  
89 sub get
90 1 ▶{
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
91 1 my $self = shift;
92 1 my $key = shift;
93 1 return $self->[DBM_ANY_HASHREF]->{$key};
94 }
95  
96 sub keys
97 1 ▶{
Subroutine keys: called CC=1 SCAR=0.0
Pod undocumented
98 1 my $self = shift;
99 1 return keys %{$self->[DBM_ANY_HASHREF]};
100 }
101  
102 sub values
103 1 ▶{
Subroutine values: called CC=1 SCAR=0.0
Pod undocumented
104 1 my $self = shift;
105 1 return values %{$self->[DBM_ANY_HASHREF]};
106 }
107  
108 sub each
109 6 ▶{
Subroutine each: called CC=1 SCAR=0.0
Pod undocumented
110 6 my $self = shift;
111 6 return each %{$self->[DBM_ANY_HASHREF]};
112 }
113  
114 sub exists
115 3 ▶{
Subroutine exists: called CC=2 SCAR=7.3
Pod undocumented
116 3 my $self = shift;
117 3 my $key = shift;
118 3 my $r;
119 3 eval { $r = exists $self->[DBM_ANY_HASHREF]->{$key}; };
120 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
121 0 $r = $self->get($key);
122 0 return defined $r;
123 }
124 3 return $r;
125 }
126  
127 sub delete
128 1 ▶{
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
129 1 my $self = shift;
130 1 my $key = shift;
131 1 return delete $self->[DBM_ANY_HASHREF]->{$key};
132 }
133  
134 sub close
135 4 ▶{
Subroutine close: called CC=1 SCAR=0.0
Pod undocumented
136 4 my $self = shift;
137 4 eval { $self->[DBM_ANY_TIEREF]->sync(); }; ## Eh, worth a shot.
138 4 untie $self->[DBM_ANY_HASHREF];
139 4 undef $self->[DBM_ANY_TIEREF];
140 }
141  
142 sub DESTROY
143 2 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
144 2 $_[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 E<lt>tmonroe+perl@nog.netE<gt>
161  
162 =head1 SEE ALSO
163  
164 L<perl>, L<AnyDBM_File>
165  
166 =cut
167  
168 2 1;
169 __END__
↑ Summary