| 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 ||= {}
MC/DC0%0/1
$lock_options ||= {}
$lock_options
Truth table50%1/2
A
A= $lock_options
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 43 | ▶ | $options ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||
| 36 | 43 | my $_self = {%defaults, %$options}; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 43 | ▶ | if ($options->{read_only} and $options->{auto_commit}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$options->{"read_only"} and $options->{"auto_commit"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | 5 | $_self->{auto_commit} = 0; | |||||||||||||||||||||||||||
| 44 | 5 | $_self->{lock} = 0; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 42 | ▶ | if ($_self->{lock}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 37 | ▶ | $lock_options = $_self->{lock} if ref $_self->{lock}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 37 | my $lock = lockfile("$fname.lock", { mode => $_self->{mode}, blocking => 1, remove => 1, %$lock_options }); | |||||||||||||||||||||||||||
| 50 | 37 | ▶ | unless (defined $lock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
-e $fname and not $_self->{"write_only"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 64 | 5 | eval $str; | |||||||||||||||||||||||||||
| 65 | 5 | ▶ | die "Can't eval $fname: $@" unless $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 5 | ▶ | die "Invalid data in $fname: $data" unless ref $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 5 | $self = $data; | |||||||||||||||||||||||||||
| 68 | 5 | ▶ | $_self->{format} = 'dumper' if $_self->{format} eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } elsif ($str =~ /^{/) { | ||||||||||||||||||||||||||||
| 70 | 14 | $self = JSON->new->decode($str); | |||||||||||||||||||||||||||
| 71 | 14 | ▶ | $_self->{format} = 'json' if $_self->{format} eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | 4 | $self = thaw($str); | |||||||||||||||||||||||||||
| 75 | 4 | ▶ | $_self->{format} = 'storable' if $_self->{format} eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||||||||||
| 78 | 18 | ▶ | $_self->{format} = 'json' if $_self->{format} eq 'auto'; # default format for new files | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 92 | 0 | croak "$_self->{fname} is already removed and can't be commited"; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 27 | ▶ | if ($_self->{read_only}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 25 | ▶ | chmod $_self->{mode}, $tmp_fname if defined $_self->{mode}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$_self->{"auto_commit"} and not $self->{"removed"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 147 | 0 | croak "Can't remove $_self->{fname}, object is read only"; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 2 | ▶ | if (-e $_self->{fname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | unlink $_self->{fname} or die "Can't remove $_self->{fname}: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 2 | ▶ | if ($_self->{lock}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 2 | my $lock_fname = $_self->{lock}->name; | |||||||||||||||||||||||||||
| 154 | 2 | ▶ | if (-e $lock_fname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 2 | ▶ | unlink $lock_fname or die "Can't remove $lock_fname: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 2 | $_self->{removed} = 1; | |||||||||||||||||||||||||||
| 159 | 2 | delete $self->{$_} for keys %$self; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | 1; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||