blib/lib/Tie/File/Hashify.pm

filter: Statementstmt 91.5% 76 / 83 Branchbran 77.7% 28 / 36 Conditioncond 70.8% 17 / 24 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 82.5% 142 / 172 CC 27 SCAR 21.1
CC
27
Cov
85%
TIEHASH
17.4
_store
6.0
SCALAR
5.2
CRAP 8.3
↑ Summary
1  
2 package Tie::File::Hashify;
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.03';
11  
12  
13 6 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=15 SCAR=28.6
14 6 my ($class, $path, %options) = @_;
15  
16 my $self = bless {
17 hash => {},
18 format => $options{format},
19 parse => $options{parse},
20 path => $path,
21 ro => $options{ro},
22 6 dirty => 0,
23 }, $class;
24  
25 6 ▶ if($path and -e $path and $options{parse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and -e $path and $options{"parse"}) 5 1 6
Condition66%4/6
$path and -e $path and $options{"parse"}
!ll&&!rl&&r
105
$path and -e $path
!ll&&!rl&&r
105
MC/DC33%1/3
$path and -e $path and $options{"parse"}
$path -e $path $options{"parse"}
Truth table50%2/4
A and B and C
A= $pathB= -e $pathC= $options{"parse"}
ABCresult
0XX0
10X0
1100
1111
26 5 ▶ my $io = new IO::File($path) or croak "Can't read $path. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $io = "IO::File"->new($path) or croak("Can't read $path. $!.\n") 5 0 5
27  
28 5 ▶ while(my $line = $io->getline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $io->getline) 24 5 29
29 24 ▶ next unless defined $line and length($line);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 0 24 24
Condition33%1/3
defined $line and length $line
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $line and length $line
defined $line length $line
Truth table33%1/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
30  
31 24 my ($key, $value);
32  
33 # Use callback for parsing.
34 24 ▶ if(ref($options{parse}) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $options{'parse'} eq 'CODE') { } 9 15 24
elsif (ref $options{'parse'} eq '' or uc ref $options{'parse'} eq 'REGEXP') { } 15 0 15
35 9 ($key, $value) = &{$options{parse}}($line);
36 }
37  
38 # Parse line using a regular expression.
39 ▶ elsif(ref($options{parse}) eq '' or uc(ref($options{parse})) eq 'REGEXP') {
Condition66%2/3
ref $options{"parse"} eq "" or uc ref $options{"parse"} eq "REGEXP"
l!l&&r!l&&!r
5100
MC/DC0%0/2
ref $options{"parse"} eq "" or uc ref $options{"parse"} eq "REGEXP"
ref $options{"parse"} eq "" uc ref $options{"parse"} eq "REGEXP"
Truth table66%2/3
A or B
A= ref $options{"parse"} eq ""B= uc ref $options{"parse"} eq "REGEXP"
ABresult
1X1
011
000
40 15 ▶ my $re = ref($options{parse}) ? $options{parse} : qr/^$options{parse}$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $options{'parse'} ? : 10 5 15
41 15 ($key, $value) = ($line =~ $re);
42 }
43  
44 # Croak.
45 else {
46 0 croak 'Can\'t use ', lc(ref($options{parse})), " for parsing.\n";
47 }
48  
49 24 ▶ if(defined $key and length $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and length $key) 16 8 24
Condition66%2/3
defined $key and length $key
!ll&&!rl&&r
8016
MC/DC50%1/2
defined $key and length $key
defined $key length $key
Truth table66%2/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
50 16 ▶ $self->{hash}->{$key} = $value if(length $key);
Branch: 1 branch
BranchTrueFalseTotal
if length $key 16 0 16
51 }
52 }
53  
54 5 $io->close;
55 }
56  
57 6 return $self;
58 }
59  
60  
61 26 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
62 26 my ($self, $key) = @_;
63 26 return $self->{hash}->{$key};
64 }
65  
66  
67 4 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=6.9
68 4 my ($self, $key, $value) = @_;
69  
70 4 ▶ croak "Can't store in read-only mode" if($self->{ro});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ro"} 1 3 4
71  
72 3 $self->{dirty} = !0;
73  
74 3 return $self->{hash}->{$key} = $value;
75 }
76  
77  
78 2 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
79 2 my ($self, $key) = @_;
80 2 return exists($self->{hash}->{$key});
81 }
82  
83  
84 2 ▶sub DELETE {
Subroutine DELETE: called CC=2 SCAR=6.9
85 2 my ($self, $key) = @_;
86  
87 2 ▶ croak "Can't delete in read-only mode" if($self->{ro});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ro"} 1 1 2
88  
89 1 $self->{dirty} = !0;
90  
91 1 return delete($self->{hash}->{$key});
92 }
93  
94  
95 1 ▶sub CLEAR {
Subroutine CLEAR: called CC=2 SCAR=10.1
96 1 my ($self) = @_;
97  
98 1 ▶ croak "Can't clear in read-only mode" if($self->{ro});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ro"} 1 0 1
99  
100 0 $self->{dirty} = !0;
101  
102 0 %{$self->{hash}} = ();
103 }
104  
105  
106 2 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
107 2 my ($self) = @_;
108 2 my ($key) = each %{$self->{hash}};
109 2 return $key;
110 }
111  
112  
113 8 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
114 8 my ($self) = @_;
115  
116 8 my ($k, $v) = each %{$self->{hash}};
117  
118 8 return $k;
119 }
120  
121  
122 2 ▶sub SCALAR {
Subroutine SCALAR: called CC=5 SCAR=16.5
123 2 my ($self) = @_;
124  
125 2 my $format = $self->{format};
126  
127 2 ▶ if(defined $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format) { } 2 0 2
128 2 my $text = '';
129  
130 2 values %{$self->{hash}};
131  
132 2 ▶ while(my ($key, $value) = each %{$self->{hash}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"hash"};}) 5 2 7
133 # Format using callback.
134 5 ▶ if(ref($format) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $format eq 'CODE') { } 4 1 5
elsif (ref $format eq '') { } 1 0 1
135 4 $text .= &{$format}($key, $value) . "\n";
136 }
137  
138 # Format using sprintf and a format string.
139 elsif(ref($format) eq '') {
140 1 $text .= sprintf($format, $key, $value) . "\n";
141 }
142  
143 # Croak.
144 else {
145 0 croak 'Can\'t use ' . ref($format) . " as format.\n";
146 }
147 }
148  
149 2 return $text;
150 }
151  
152 else {
153 0 return %{$self->{hash}};
154 }
155 }
156  
157  
158 12 ▶sub _store {
Subroutine _store: called CC=6 SCAR=18.0
159 12 my ($self) = @_;
160  
161 12 my $path = $self->{path};
162  
163 12 ▶ if($path and $self->{dirty} and $self->{format} and !$self->{ro}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and $self->{"dirty"} and $self->{"format"} and not $self->{"ro"}) 1 11 12
Condition88%8/9
$path and $self->{"dirty"} and $self->{"format"} and not $self->{"ro"}
!ll&&!rl&&r
1101
$path and $self->{"dirty"} and $self->{"format"}
!ll&&!rl&&r
921
$path and $self->{"dirty"}
!ll&&!rl&&r
273
MC/DC75%3/4
$path and $self->{"dirty"} and $self->{"format"} and not $self->{"ro"}
$path $self->{"dirty"} $self->{"format"} not $self->{"ro"}
Truth table80%4/5
A and B and C and not(D)
A= $pathB= $self->{"dirty"}C= $self->{"format"}D= not $self->{"ro"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
164 1 ▶ my $io = new IO::File('>' . $path) or croak "Can't write $path. $!.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $io = "IO::File"->new('>' . $path) or croak("Can't write $path. $!.\n") 1 0 1
165  
166 1 $io->print($self->SCALAR);
167 1 $io->close;
168  
169 1 $self->{dirty} = 0;
170 }
171 }
172  
173  
174 6 ▶sub UNTIE {
Subroutine UNTIE: called CC=1 SCAR=0.0
175 6 my ($self) = @_;
176  
177 6 $self->_store;
178 }
179  
180  
181 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
182 6 my ($self) = @_;
183  
184 6 $self->_store;
185 }
186  
187  
188 4 !0;
189  
190 __END__
↑ Summary