lib/Hash/Persistent.pm

filter: Statementstmt 92.7% 103 / 111 Branchbran 78.5% 44 / 56 Conditioncond 76.9% 10 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 87.1% 177 / 203 CC 35 SCAR 24.3
CC
35
Cov
87%
new
19.0
remove
9.0
DESTROY
7.7
CRAP 11.3
« blib/lib/Hash/Persistent.pm ↑ Summary blib/lib/Hash/Persistent/Memory.pm »
1 package Hash::Persistent;
2  
3 2 our $VERSION = '1.01'; # VERSION
4 # ABSTRACT: nested hashref serializable to the file
5  
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use autodie qw( open close chmod rename );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Storable qw(thaw nfreeze);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Lock::File 1.01 qw(lockfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 my %defaults = (
20 read_only => 0,
21 auto_commit => 0,
22 format => 'auto',
23 lock => 1,
24 write_only => 0,
25 );
26  
27 2 my $meta = {};
28  
29 sub new {
30 43 ▶ my $class = shift;
Subroutine new: called CC=19 SCAR=29.5
Pod documented
31 43 my ($fname, $options, $lock_options) = @_;
32  
33 43 ▶ $lock_options ||= {};
Condition50%1/2
$lock_options ||= {}
l!l
043
MC/DC0%0/1
$lock_options ||= {}
$lock_options
Truth table50%1/2
A
A= $lock_options
Aresult
11
00
34  
35 43 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
2914
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
36 43 my $_self = {%defaults, %$options};
37  
38 43 ▶ if ($options->{read_only} and $options->{auto_commit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"read_only"} and $options->{"auto_commit"}) 1 42 43
Condition100%3/3
$options->{"read_only"} and $options->{"auto_commit"}
!ll&&!rl&&r
3751
MC/DC100%2/2
$options->{"read_only"} and $options->{"auto_commit"}
$options->{"read_only"} $options->{"auto_commit"}
Truth table100%3/3
A and B
A= $options->{"read_only"}B= $options->{"auto_commit"}
ABresult
0X0
100
111
39 1 croak "Only one of 'read_only' and 'auto_commit' options can be true";
40 }
41  
42 42 ▶ if ($_self->{read_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_self->{"read_only"}) 5 37 42
43 5 $_self->{auto_commit} = 0;
44 5 $_self->{lock} = 0;
45 }
46  
47 42 ▶ if ($_self->{lock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_self->{"lock"}) 37 5 42
48 37 ▶ $lock_options = $_self->{lock} if ref $_self->{lock};
Branch: 1 branch
BranchTrueFalseTotal
if ref $_self->{"lock"} 2 35 37
49 37 my $lock = lockfile("$fname.lock", { mode => $_self->{mode}, blocking => 1, remove => 1, %$lock_options });
50 37 ▶ unless (defined $lock) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lock) 1 36 37
51 1 return;
52 }
53 36 $_self->{lock} = $lock;
54 }
55 41 $_self->{fname} = $fname;
56  
57 41 my $self;
58 41 ▶ if (-e $fname and not $_self->{write_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fname and not $_self->{'write_only'}) { } 23 18 41
Condition100%3/3
-e $fname and not $_self->{"write_only"}
!ll&&!rl&&r
15323
MC/DC100%2/2
-e $fname and not $_self->{"write_only"}
-e $fname not $_self->{"write_only"}
Truth table100%3/3
A and not(B)
A= -e $fnameB= not $_self->{"write_only"}
ABresult
0X0
100
111
59 23 open my $fh, '<', $fname;
60 23 my $data;
61 23 local $/;
62 23 my $str = <$fh>;
63 23 ▶ if ($str =~ m{^\$data = }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /^\$data = /) { } 5 18 23
elsif ($str =~ /^{/) { } 14 4 18
64 5 eval $str;
65 5 ▶ die "Can't eval $fname: $@" unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 5 5
66 5 ▶ die "Invalid data in $fname: $data" unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 5 5
67 5 $self = $data;
68 5 ▶ $_self->{format} = 'dumper' if $_self->{format} eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $_self->{"format"} eq "auto" 2 3 5
69 } elsif ($str =~ /^{/) {
70 14 $self = JSON->new->decode($str);
71 14 ▶ $_self->{format} = 'json' if $_self->{format} eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $_self->{"format"} eq "auto" 13 1 14
72 }
73 else {
74 4 $self = thaw($str);
75 4 ▶ $_self->{format} = 'storable' if $_self->{format} eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $_self->{"format"} eq "auto" 2 2 4
76 }
77 } else {
78 18 ▶ $_self->{format} = 'json' if $_self->{format} eq 'auto'; # default format for new files
Branch: 1 branch
BranchTrueFalseTotal
if $_self->{"format"} eq "auto" 7 11 18
79 18 $self = {};
80 }
81  
82 41 bless $self => $class;
83 41 $meta->{$self} = $_self;
84 41 return $self;
85 }
86  
87 sub commit {
88 27 ▶ my $self = shift;
Subroutine commit: called CC=7 SCAR=19.5
Pod documented
89 27 my $_self = $meta->{$self};
90  
91 27 ▶ if ($_self->{removed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_self->{"removed"}) 0 27 27
92 0 croak "$_self->{fname} is already removed and can't be commited";
93 }
94 27 ▶ if ($_self->{read_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_self->{"read_only"}) 1 26 27
95 1 croak "Can't commit to $_self->{fname}, object is read only";
96 }
97  
98 26 my $fname = $_self->{fname};
99 26 my $tmp_fname = "$fname.tmp";
100 26 open my $tmp, '>', $tmp_fname;
101  
102 26 my $serialized;
103 26 ▶ if ($_self->{format} eq 'dumper') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_self->{'format'} eq 'dumper') { } 6 20 26
elsif ($_self->{'format'} eq 'json') { } 13 7 20
104 6 my $dumper = Data::Dumper->new([ { %$self } ], [ qw(data) ]);
105 6 $dumper->Terse(0); # somebody could enable terse mode globally; TODO - explicitly specify other options too?
106 6 $dumper->Purity(1);
107 6 $dumper->Sortkeys(1);
108 6 $serialized = $dumper->Dump;
109 }
110 elsif ($_self->{format} eq 'json') {
111 13 $serialized = JSON->new->encode({ %$self });
112 }
113 else {
114 7 $serialized = nfreeze({ %$self });
115 }
116 25 ▶ print {$tmp} $serialized or die "print failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$tmp;} $serialized or die "print failed: $!" 0 25 25
117  
118 25 ▶ chmod $_self->{mode}, $tmp_fname if defined $_self->{mode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_self->{"mode"} 1 24 25
119 25 rename $tmp_fname => $fname;
120 }
121  
122 sub DESTROY {
123 41 ▶ local $@;
Subroutine DESTROY: called CC=4 SCAR=20.3
124 41 my $self = shift;
125  
126 41 my $_self = $meta->{$self};
127 41 ▶ if ($_self->{auto_commit} and not $self->{removed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_self->{'auto_commit'} and not $self->{'removed'}) { } 0 41 41
Condition33%1/3
$_self->{"auto_commit"} and not $self->{"removed"}
!ll&&!rl&&r
4100
MC/DC0%0/2
$_self->{"auto_commit"} and not $self->{"removed"}
$_self->{"auto_commit"} not $self->{"removed"}
Truth table33%1/3
A and not(B)
A= $_self->{"auto_commit"}B= not $self->{"removed"}
ABresult
0X0
100
111
128 0 my $commited = eval {
129 0 $self->commit();
130 0 1;
131 };
132 0 delete $meta->{$self}; # delete object anyway, commited or not
133 0 ▶ unless ($commited) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($commited) 0 0 0
134 0 ERROR $@;
135 }
136 }
137 else {
138 41 delete $meta->{$self};
139 }
140 }
141  
142 sub remove {
143 2 ▶ my $self = shift;
Subroutine remove: called CC=8 SCAR=22.0
Pod documented
144  
145 2 my $_self = $meta->{$self};
146 2 ▶ if ($_self->{read_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_self->{"read_only"}) 0 2 2
147 0 croak "Can't remove $_self->{fname}, object is read only";
148 }
149 2 ▶ if (-e $_self->{fname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $_self->{"fname"}) 1 1 2
150 1 ▶ unlink $_self->{fname} or die "Can't remove $_self->{fname}: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $_self->{"fname"} or die qq[Can't remove $_self->{"fname"}: $!] 0 1 1
151 }
152 2 ▶ if ($_self->{lock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_self->{"lock"}) 2 0 2
153 2 my $lock_fname = $_self->{lock}->name;
154 2 ▶ if (-e $lock_fname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $lock_fname) 2 0 2
155 2 ▶ unlink $lock_fname or die "Can't remove $lock_fname: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $lock_fname or die "Can't remove ${lock_fname}: $!" 0 2 2
156 }
157 }
158 2 $_self->{removed} = 1;
159 2 delete $self->{$_} for keys %$self;
160 }
161  
162 2 1;
163  
164 __END__
« blib/lib/Hash/Persistent.pm ↑ Summary blib/lib/Hash/Persistent/Memory.pm »