Brackup/Dict/DBM.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 74 CC 5 SCAR 23.7
CC
5
Cov
0%
each
30.0
BEGIN
2.0
new
2.0
CRAP 10.8
1 package Brackup::Dict::DBM;
2 use strict;
3 use warnings;
4 BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File SDBM_File) }
5 use AnyDBM_File;
6 use Fcntl qw(O_CREAT O_RDWR);
7  
8 sub new {
9 my ($class, %opts) = @_;
10 my $self = bless {
11 file => $opts{file},
12 data => {},
13 }, $class;
14  
15 my %dbm;
16 tie %dbm, 'AnyDBM_File', $self->{file}, O_CREAT | O_RDWR, 0644 or
17 die "Failed to bind to DBM digest cache at $self->{file}: $!";
18 $self->{dbm} = \%dbm;
19  
20 return $self;
21 }
22  
23 sub get {
24 my ($self, $key) = @_;
25 return $self->{dbm}->{$key};
26 }
27  
28 sub set {
29 my ($self, $key, $val) = @_;
30 $self->{dbm}->{$key} = $val;
31 return 1;
32 }
33  
34 # Iterator interface, returning ($key, $value), and () on eod
35 sub each {
36 my $self = shift;
37 $self->{keys} = [ keys %{$self->{dbm}} ] unless $self->{_loaded_keys}++;
38 if (! @{$self->{keys}}) {
39 $self->{_loaded_keys} = 0;
40 return wantarray ? () : undef;
41 }
42 my $next = shift @{$self->{keys}};
43 return wantarray ? ($next, $self->{dbm}->{$next}) : $next;
44 }
45  
46 sub delete {
47 my ($self, $key) = @_;
48 delete $self->{dbm}->{$key};
49 return 1;
50 }
51  
52 sub count {
53 my $self = shift;
54 return scalar keys %{$self->{dbm}};
55 }
56  
57 sub backing_file {
58 my $self = shift;
59 return $self->{file};
60 }
61  
62 1;
63