lib/File/DataClass/Functions.pm

filter: Statementstmt 96.8% 182 / 188 Branchbran 89.7% 67 / 78 (3 uncoverable) Conditioncond 82.2% 51 / 62 MC/DCmcdc 71.0% 27 / 38 Subroutinesub 100% 38 / 38 Podpod 100% 18 / 18 totaltotal 91.4% 383 / 422 (3 uncoverable) CC 70 SCAR 17.8
CC
70
Cov
92%
__ANON__
14.8
merge_for_update
9.0
extension_map
8.0
CRAP 6.0
↑ Summary lib/File/DataClass/IO.pm »
1 package File::DataClass::Functions;
2  
3 7 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Exporter 5.57 qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use File::DataClass::Constants qw( CYGWIN EXCEPTION_CLASS MSOFT STORAGE_BASE
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 STORAGE_EXCEPTIONS );
11 7 ▶use Hash::Merge qw( merge );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Module::Runtime qw( require_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Unexpected::Functions qw( is_class_loaded Unspecified );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 our @EXPORT_OK = qw( ensure_class_loaded extension_map first_char
20 is_arrayref is_coderef is_hashref is_member is_mswin
21 is_ntfs is_stale qualify_storage_class
22 map_extension2class merge_attributes merge_file_data
23 merge_for_update supported_extensions thread_id throw );
24 7 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ], );
25  
26 7 my $LC_OSNAME = lc $OSNAME;
27  
28 # Private functions
29 7 my $_merge_attr;
30  
31 2 ▶my $_merge_attr_arrays = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
32 2 my ($to, $from) = @_; my $updated = 0;
33  
34 2 for (0 .. $#{ $to }) {
35 3 ▶ if (defined $from->[ $_ ]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $from->[$_]) { } 2 1 3
elsif ($to->[$_]) { } 1 0 1
36 2 my $res = $_merge_attr->( \$to->[ $_ ], $from->[ $_ ] );
37  
38 2 ▶ $updated ||= $res;
Condition33%1/3
$updated ||= $res
l!l&&r!l&&!r
002
MC/DC0%0/2
$updated ||= $res
$updated $res
Truth table33%1/3
A ||= B
A= $updatedB= $res
ABresult
1X1
011
000
39 }
40 1 elsif ($to->[ $_ ]) { splice @{ $to }, $_; $updated = 1; last }
41 }
42  
43 2 ▶ if (@{ $from } > @{ $to }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$from;} > @{$to;}) 1 1 2
44 1 push @{ $to }, (splice @{ $from }, $#{ $to } + 1); $updated = 1;
45 }
46  
47 2 return $updated;
48 7 };
49  
50 2 ▶my $_merge_attr_hashes = sub {
Subroutine __ANON__: called CC=6 SCAR=19.2
51 2 my ($to, $from) = @_; my $updated = 0;
52  
53 2 for (grep { exists $from->{ $_ } } keys %{ $to }) {
54 2 ▶ if (defined $from->{ $_ }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from->{$_}) { } 1 1 2
55 1 my $res = $_merge_attr->( \$to->{ $_ }, $from->{ $_ } );
56  
57 1 ▶ $updated ||= $res;
Condition33%1/3
$updated ||= $res
l!l&&r!l&&!r
010
MC/DC0%0/2
$updated ||= $res
$updated $res
Truth table33%1/3
A ||= B
A= $updatedB= $res
ABresult
1X1
011
000
58 }
59 1 else { delete $to->{ $_ }; delete $from->{ $_ }; $updated = 1 }
60 }
61  
62 2 for (grep { not exists $to->{ $_ } } keys %{ $from }) {
63 0 ▶ if (defined $from->{ $_ }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from->{$_}) 0 0 0
64 0 $to->{ $_ } = $from->{ $_ }; $updated = 1;
65 }
66 }
67  
68 2 return $updated;
69 7 };
70  
71 25 ▶$_merge_attr = sub {
Subroutine __ANON__: called CC=14 SCAR=26.9
72 25 my ($to_ref, $from) = @_; my $to = ${ $to_ref }; my $updated = 0;
73  
74 25 ▶ if ($to and ref $to eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if ($to and ref $to eq 'HASH') { } 2 23 25
elsif ($to and ref $to eq 'ARRAY') { } 2 21 23
elsif (defined $to and $to ne $from) { } 7 14 21
elsif (not defined $to) { } 7 7 14
Condition100%3/3
$to and ref $to eq "HASH"
!ll&&!rl&&r
9142
MC/DC100%2/2
$to and ref $to eq "HASH"
$to ref $to eq "HASH"
Truth table100%3/3
A and B
A= $toB= ref $to eq "HASH"
ABresult
0X0
100
111
75 2 $updated = $_merge_attr_hashes->( $to, $from );
76 }
77 ▶ elsif ($to and ref $to eq 'ARRAY') {
Condition100%3/3
$to and ref $to eq "ARRAY"
!ll&&!rl&&r
9122
MC/DC100%2/2
$to and ref $to eq "ARRAY"
$to ref $to eq "ARRAY"
Truth table100%3/3
A and B
A= $toB= ref $to eq "ARRAY"
ABresult
0X0
100
111
78 2 $updated = $_merge_attr_arrays->( $to, $from );
79 }
80 ▶ elsif (defined $to and $to ne $from) {
Condition100%3/3
defined $to and $to ne $from
!ll&&!rl&&r
777
MC/DC100%2/2
defined $to and $to ne $from
defined $to $to ne $from
Truth table100%3/3
A and B
A= defined $toB= $to ne $from
ABresult
0X0
100
111
81 7 $updated = 1; ${ $to_ref } = $from;
82 }
83 elsif (not defined $to) {
84 7 ▶ if (ref $from eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $from eq 'HASH') { } 2 5 7
elsif (ref $from eq 'ARRAY') { } 0 5 5
85 2 scalar keys %{ $from } > 0 and $updated = 1
86 2 ▶ and ${ $to_ref } = $from;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$from;} > 0 and $updated = 1 and ${$to_ref;} = $from 1 1 2
Condition100%2/2
scalar keys %{$from;} > 0 and $updated = 1
!ll
11
MC/DC100%1/1
scalar keys %{$from;} > 0 and $updated = 1
scalar keys %{$from;} > 0
Truth table100%2/2
A
A= scalar keys %{$from;} > 0
Aresult
00
11
87 }
88 elsif (ref $from eq 'ARRAY') {
89 0 ▶ scalar @{ $from } > 0 and $updated = 1 and ${ $to_ref } = $from;
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$from;} > 0 and $updated = 1 and ${$to_ref;} = $from 0 0 0
Condition0%0/2
scalar @{$from;} > 0 and $updated = 1
!ll
00
MC/DC0%0/1
scalar @{$from;} > 0 and $updated = 1
scalar @{$from;} > 0
Truth table0%0/2
A
A= scalar @{$from;} > 0
Aresult
00
11
90 }
91 5 else { $updated = 1; ${ $to_ref } = $from }
92 }
93  
94 25 return $updated;
95 7 };
96  
97 # Public functions
98 42 ▶sub ensure_class_loaded ($;$) {
Subroutine ensure_class_loaded: called CC=5 SCAR=16.1
Pod documented
99 42 ▶ my ($class, $opts) = @_; $opts //= {};
Condition100%2/2
$opts //= {}
l!l
141
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
100  
101 42 ▶ not $opts->{ignore_loaded} and is_class_loaded( $class ) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
not $opts->{"ignore_loaded"} and &is_class_loaded($class) and return 1 33 9 42
Condition100%3/3
not $opts->{"ignore_loaded"} and &is_class_loaded($class)
!ll&&!rl&&r
1833
MC/DC100%2/2
not $opts->{"ignore_loaded"} and &is_class_loaded($class)
not $opts->{"ignore_loaded"} &is_class_loaded($class)
Truth table100%3/3
not(A) and B
A= not $opts->{"ignore_loaded"}B= &is_class_loaded($class)
ABresult
0X0
100
111
102  
103 9 ▶ try { require_module( $class ) } catch { throw( $_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
104  
105 8 ▶ is_class_loaded( $class )
Branch: 1 branch
BranchTrueFalseTotal
&is_class_loaded($class) or &throw("Class [_1] loaded but package undefined", [$class]) 7 1 8
106 or throw( 'Class [_1] loaded but package undefined', [ $class ] );
107  
108 7 return 1;
109 }
110  
111 7 { my $_extension_map = { '_map_loaded' => 0 };
112  
113 17 ▶ sub extension_map (;$$) {
Subroutine extension_map: called CC=8 SCAR=20.8
Pod documented
114 17 my ($class, $extensions) = @_;
115  
116 17 ▶ if (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 5 12 17
117 5 ▶ if (defined $extensions) { # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extensions) 5 0 5
118 5 ▶ is_arrayref( $extensions ) or $extensions = [ $extensions ];
Branch: 1 branch
BranchTrueFalseTotal
&is_arrayref($extensions) or $extensions = [$extensions] 1 4 5
119  
120 5 for my $extn (@{ $extensions }) {
121 6 ▶ $_extension_map->{ $extn } //= [];
Condition100%2/2
$_extension_map->{$extn} //= []
l!l
15
MC/DC100%1/1
$_extension_map->{$extn} //= []
$_extension_map->{$extn}
Truth table100%2/2
A
A= $_extension_map->{$extn}
Aresult
11
00
122 is_member( $class, $_extension_map->{ $extn } )
123 6 ▶ or push @{ $_extension_map->{ $extn } }, $class;
Branch: 1 branch
BranchTrueFalseTotal
&is_member($class, $_extension_map->{$extn}) or push @{$$_extension_map{$extn};}, $class 1 5 6
124 }
125 }
126  
127 5 return;
128 }
129  
130 12 ▶ $_extension_map->{ '_map_loaded' } and return $_extension_map;
Branch: 1 branch
BranchTrueFalseTotal
$_extension_map->{"_map_loaded"} and return $_extension_map 9 3 12
131  
132 3 my $base = STORAGE_BASE;
133 3 my $exceptions = STORAGE_EXCEPTIONS;
134 3 my $finder = Module::Pluggable::Object->new
135 ( except => [ $exceptions ], search_path => [ $base ], require => 1, );
136  
137 3 $finder->plugins; $_extension_map->{ '_map_loaded' } = 1;
138  
139 3 return $_extension_map;
140 }
141 }
142  
143 7 ▶sub first_char ($) {
Subroutine first_char: called CC=1 SCAR=0.0
Pod documented
144 1493 return substr $_[ 0 ], 0, 1;
145 }
146  
147 737 ▶sub is_arrayref (;$) {
Subroutine is_arrayref: called CC=3 SCAR=11.0
Pod documented
148 737 ▶ return $_[ 0 ] && ref $_[ 0 ] eq 'ARRAY' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'ARRAY' ? : 441 296 737
Condition100%3/3
$_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
24272441
MC/DC100%2/2
$_[0] and ref $_[0] eq "ARRAY"
$_[0] ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
149 }
150  
151 647 ▶sub is_coderef (;$) {
Subroutine is_coderef: called CC=3 SCAR=11.0
Pod documented
152 647 ▶ return $_[ 0 ] && ref $_[ 0 ] eq 'CODE' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'CODE' ? : 1 646 647
Condition100%3/3
$_[0] and ref $_[0] eq "CODE"
!ll&&!rl&&r
246221
MC/DC100%2/2
$_[0] and ref $_[0] eq "CODE"
$_[0] ref $_[0] eq "CODE"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
153 }
154  
155 1025 ▶sub is_hashref (;$) {
Subroutine is_hashref: called CC=3 SCAR=11.0
Pod documented
156 1025 ▶ return $_[ 0 ] && ref $_[ 0 ] eq 'HASH' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'HASH' ? : 428 597 1025
Condition100%3/3
$_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
2595428
MC/DC100%2/2
$_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
157 }
158  
159 34 ▶sub is_member (;@) {
Subroutine is_member: called CC=4 SCAR=13.9
Pod documented
160 34 ▶ my ($candidate, @args) = @_; $candidate or return;
Branch: 1 branch
BranchTrueFalseTotal
$candidate or return 33 1 34
161  
162 33 ▶ is_arrayref $args[ 0 ] and @args = @{ $args[ 0 ] };
Branch: 1 branch
BranchTrueFalseTotal
&is_arrayref($args[0]) and @args = @{$args[0];} 31 2 33
163  
164 33 ▶ return (first { $_ eq $candidate } @args) ? 1 : 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
&first(sub { $_ eq $candidate; } , @args) ? : 13 20 33
165 }
166  
167 545 ▶sub is_mswin () {
Subroutine is_mswin: called CC=2 SCAR=7.6
Pod documented
168 545 ▶ return $LC_OSNAME eq MSOFT ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$LC_OSNAME eq 'mswin32' ? : 0 545 545
169 }
170  
171 506 ▶sub is_ntfs () {
Subroutine is_ntfs: called CC=3 SCAR=14.2
Pod documented
172 506 ▶ return is_mswin || $LC_OSNAME eq CYGWIN ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&is_mswin() || $LC_OSNAME eq 'cygwin' ? : 0 506 506
Condition33%1/3
&is_mswin() or $LC_OSNAME eq "cygwin"
l!l&&r!l&&!r
00506
MC/DC0%0/2
&is_mswin() or $LC_OSNAME eq "cygwin"
&is_mswin() $LC_OSNAME eq "cygwin"
Truth table33%1/3
A or B
A= &is_mswin()B= $LC_OSNAME eq "cygwin"
ABresult
1X1
011
000
173 }
174  
175 86 ▶sub is_stale (;$$$) {
Subroutine is_stale: called CC=6 SCAR=17.9
Pod documented
176 86 my ($data, $cache_mtime, $path_mtime) = @_;
177  
178 # Assume NTFS does not support mtime
179 86 ▶ is_ntfs() and return 1; # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
&is_ntfs() and return 1 0 86 86
180  
181 86 ▶ my $is_def = defined $data && defined $path_mtime && defined $cache_mtime;
Condition83%5/6
defined $data && defined $path_mtime && defined $cache_mtime
!ll&&!rl&&r
281048
defined $data and defined $path_mtime
!ll&&!rl&&r
28058
MC/DC66%2/3
defined $data && defined $path_mtime && defined $cache_mtime
defined $data defined $path_mtime defined $cache_mtime
Truth table75%3/4
A and B && C
A= defined $dataB= defined $path_mtimeC= defined $cache_mtime
ABCresult
0XX0
10X0
1100
1111
182  
183 86 ▶ return (!$is_def || ($path_mtime > $cache_mtime)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!$is_def || $path_mtime > $cache_mtime ? : 39 47 86
Condition100%3/3
not $is_def or $path_mtime > $cache_mtime
l!l&&r!l&&!r
38147
MC/DC100%2/2
not $is_def or $path_mtime > $cache_mtime
not $is_def $path_mtime > $cache_mtime
Truth table100%3/3
not(A) or B
A= not $is_defB= $path_mtime > $cache_mtime
ABresult
1X1
011
000
184 }
185  
186 8 ▶sub map_extension2class ($) {
Subroutine map_extension2class: called CC=2 SCAR=6.9
Pod documented
187 8 my $map = extension_map();
188  
189 8 ▶ return exists $map->{ $_[ 0 ] } ? $map->{ $_[ 0 ] } : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $$map{$_[0]} ? : 7 1 8
190 }
191  
192 7 ▶sub merge_attributes ($$;$) {
Subroutine merge_attributes: called CC=7 SCAR=19.5
Pod documented
193 7 my ($dest, $src, $attrs) = @_; my $class = blessed $src;
194  
195 7 ▶ for (grep { not exists $dest->{ $_ } or not defined $dest->{ $_ } }
Condition66%2/3
exists $dest->{$_} && defined $dest->{$_}
!ll&&!rl&&r
1301
MC/DC50%1/2
exists $dest->{$_} && defined $dest->{$_}
exists $dest->{$_} defined $dest->{$_}
Truth table66%2/3
A && B
A= exists $dest->{$_}B= defined $dest->{$_}
ABresult
0X0
100
111
196 7 ▶ @{ $attrs || [] }) {
Condition100%2/2
$attrs || []
l!l
61
MC/DC100%1/1
$attrs || []
$attrs
Truth table100%2/2
A
A= $attrs
Aresult
11
00
197 13 ▶ my $v = $class ? ($src->can( $_ ) ? $src->$_() : undef) : $src->{ $_ };
Branch: 2 branches
BranchTrueFalseTotal
$class ? : 8 5 13
$src->can($_) ? : 4 4 8
198  
199 13 ▶ defined $v and $dest->{ $_ } = $v;
Branch: 1 branch
BranchTrueFalseTotal
defined $v and $dest->{$_} = $v 5 8 13
200 }
201  
202 7 return $dest;
203 }
204  
205 5 ▶sub merge_file_data ($$) {
Subroutine merge_file_data: called CC=3 SCAR=11.0
Pod documented
206 5 my ($existing, $new) = @_;
207  
208 5 for (keys %{ $new }) {
209 $existing->{ $_ } = exists $existing->{ $_ }
210 ? merge( $existing->{ $_ }, $new->{ $_ } )
211 11 ▶ : $new->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
exists $existing->{$_} ? : 1 10 11
212 }
213  
214 5 return;
215 }
216  
217 21 ▶sub merge_for_update (;$$$) {
Subroutine merge_for_update: called CC=9 SCAR=22.0
Pod documented
218 21 my ($dest_ref, $src, $filter) = @_; my $updated = 0;
219  
220 21 ▶ $dest_ref or throw( Unspecified, [ 'destination reference' ] );
Branch: 1 branch
BranchTrueFalseTotal
$dest_ref or &throw(Unspecified(), ["destination reference"]) 20 1 21
221  
222 20 ▶ ${ $dest_ref } //= {}; $src //= {}; $filter //= sub { keys %{ $_[ 0 ] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition85%6/7
${$dest_ref;} //= {}
l!l
146
$src //= {}
l!l
191
$filter //= sub { keys %{$_[0];}; }
l!l&&r!l&&!r
1730
MC/DC100%1/1
${$dest_ref;} //= {}
${$dest_ref;}
MC/DC100%1/1
$src //= {}
$src
MC/DC0%0/2
$filter //= sub { keys %{$_[0];}; }
$filter sub { keys %{$_[0];}; }
Truth table100%2/2
A
A= ${$dest_ref;}
Aresult
11
00
Truth table100%2/2
A
A= $src
Aresult
11
00
Truth table66%2/3
A //= B
A= $filterB= sub { keys %{$_[0];}; }
ABresult
1X1
011
000
223  
224 20 for my $k ($filter->( $src )) {
225 24 ▶ if (defined $src->{ $k }) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $src->{$k}) { } 22 2 24
elsif (exists ${$dest_ref;}->{$k}) { } 1 1 2
226 22 my $res = $_merge_attr->( \${ $dest_ref }->{ $k }, $src->{ $k } );
227  
228 22 ▶ $updated ||= $res;
Condition100%3/3
$updated ||= $res
l!l&&r!l&&!r
3154
MC/DC100%2/2
$updated ||= $res
$updated $res
Truth table100%3/3
A ||= B
A= $updatedB= $res
ABresult
1X1
011
000
229 }
230 2 elsif (exists ${ $dest_ref }->{ $k }) {
231 1 delete ${ $dest_ref }->{ $k }; $updated = 1;
232 }
233 }
234  
235 20 return $updated;
236 }
237  
238 21 ▶sub qualify_storage_class ($) {
Subroutine qualify_storage_class: called CC=1 SCAR=0.0
Pod documented
239 21 return STORAGE_BASE.'::'.$_[ 0 ];
240 }
241  
242 1 ▶sub supported_extensions () {
Subroutine supported_extensions: called CC=1 SCAR=0.0
Pod documented
243 1 return grep { not m{ \A _ }mx } keys %{ extension_map() };
244 }
245  
246 111 ▶sub thread_id () {
Subroutine thread_id: called CC=2 SCAR=6.9
Pod documented
247 # uncoverable branch true
248 111 ▶ return exists $INC{ 'threads.pm' } ? threads->tid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $INC{'threads.pm'} ? : 0 111 111
249 }
250  
251 48 ▶sub throw (;@) {
Subroutine throw: called CC=1 SCAR=0.0
Pod documented
252 48 EXCEPTION_CLASS->throw( @_ );
253 }
254  
255 7 1;
256  
257 __END__
↑ Summary lib/File/DataClass/IO.pm »