File Coverage

lib/Data/Dumper/Store.pm
Criterion Covered Total %
statement 44 46 95.6
branch 9 14 64.2
condition 2 6 33.3
subroutine 11 12 91.6
pod 6 6 100.0
total 72 84 85.7


line stmt bran cond sub pod time code
1             package Data::Dumper::Store;
2              
3 1     1   4394 use strict;
  1         2  
  1         30  
4 1     1   5 use warnings;
  1         1  
  1         30  
5 1     1   21 use 5.010;
  1         7  
  1         41  
6              
7 1     1   1194 use Data::Dumper;
  1         10513  
  1         724  
8              
9             $Data::Dumper::Purity = 1;
10             $Data::Dumper::Terse = 1;
11              
12             =head1 NAME
13              
14             Data::Dumper::Store - persistent key-value storage engine based on Data::Dumper serialization mechanism and flat files.
15              
16             =head1 VERSION
17              
18             Version 1.01
19              
20             =cut
21              
22             our $VERSION = '1.01';
23              
24             sub new {
25 7     7 1 2040 my ($class, %opts) = @_;
26              
27 7 100       34 defined $opts{file} or die "File is not specified";
28              
29 6         23 my $self = {
30             file => $opts{file},
31             data => {},
32             };
33 6         19 _init($self);
34              
35 6         44 return bless $self, $class;
36             }
37              
38             sub _init {
39 6     6   11 my ($self) = @_;
40              
41 6 100       112 return unless -e $self->{file};
42 5 50       163 open my $fh, '<', $self->{file}
43             or return;
44 5         6 my $text = do { local $/; <$fh> };
  5         40  
  5         110  
45 5         427 my $data = eval($text);
46              
47 5 50       21 return if $@;
48              
49 5         66 $self->{data} = $data;
50             }
51              
52             sub init {
53 2     2 1 5 my ($self, $data) = @_;
54              
55 2 50 33     27 defined $data && ref $data && ref $data eq 'HASH' or return;
      33        
56 2         6 $self->{data} = $data;
57              
58 2         8 return $self;
59             }
60              
61             sub set {
62 2     2 1 5 my ($self, $key, $val) = @_;
63              
64 2         705 $self->{data}{$key} = $val;
65              
66 2         9 return $self;
67             }
68              
69             sub get {
70 5     5 1 12 my ($self, $key) = @_;
71              
72 5 50       15 return unless $key;
73              
74 5         24 return $self->{data}{$key};
75             }
76              
77             sub dump {
78 0     0 1 0 my ($self) = @_;
79              
80 0         0 return Dumper $self->{data};
81             }
82              
83             sub commit {
84 7     7 1 11 my ($self) = @_;
85              
86 7 50       609 open my $fh, ">", $self->{file} or return;
87              
88 7         9 print {$fh} Dumper $self->{data};
  7         34  
89 7         925 close $fh;
90              
91 7         140 return 1;
92             }
93              
94             sub DESTROY {
95 6     6   3132 my ($self) = @_;
96              
97 6         18 $self->commit();
98             }
99              
100             1;
101              
102             __END__