blib/lib/IO/Any.pm

filter: Statementstmt 92.5% 136 / 147 Branchbran 76.8% 63 / 82 Conditioncond 83.3% 15 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 21 / 21 Podpod 100% 5 / 5 totaltotal 87.3% 248 / 284 CC 50 SCAR 26.6
CC
50
Cov
87%
new
25.4
_guess_what
19.0
_discard_atomic
5.0
CRAP 14.2
↑ Summary
1 package IO::Any;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.10';
8  
9 2 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::AtomicFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use List::Util qw(none any);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 31 ▶sub new {
Subroutine new: called CC=23 SCAR=32.4
Pod documented
19 31 my $class = shift;
20 31 my $what = shift;
21 31 ▶ my $how = shift || '<';
Condition50%1/2
shift() || '<'
l!l
310
MC/DC0%0/1
shift() || '<'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
22 31 ▶ my $opt = shift || {};
Condition100%2/2
shift() || {}
l!l
1714
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
23 31 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 31 31
24 if @_;
25  
26 31 ▶ confess '$what is missing'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $what 2 29 31
27 if not defined $what;
28  
29 29 ▶ confess 'expecting hash ref'
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt ne "HASH" 0 29 29
30 if ref $opt ne 'HASH';
31 29 foreach my $key (keys %$opt) {
32 confess 'unknown option '.$key
33 25 ▶ if (none { $key eq $_ } qw(atomic LOCK_SH LOCK_EX LOCK_NB BINMODE));
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $key eq $_; } , "atomic", "LOCK_SH", "LOCK_EX", "LOCK_NB", "BINMODE") 2 23 25
34 }
35  
36 27 my ($type, $proper_what) = $class->_guess_what($what);
37  
38  
39 27 ▶ if ($type eq 'string') { return IO::String->new($proper_what) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string") 3 24 27
40 24 ▶ if ($type eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "file") 23 1 24
41 23 ▶ my $fh = $opt->{'atomic'} ? IO::AtomicFile->new() : IO::File->new();
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'atomic'} ? : 5 18 23
42 23 ▶ $fh->open($proper_what, $how)
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($proper_what, $how) or confess('error opening file "' . $proper_what . '" - ' . $!) 23 0 23
43 or confess 'error opening file "'.$proper_what.'" - '.$!;
44 1 ▶ if (defined $opt->{BINMODE} and not binmode($fh, $opt->{BINMODE})) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"BINMODE"} and not binmode($fh, $opt->{"BINMODE"})) 1 22 23
Condition100%3/3
defined $opt->{"BINMODE"} and not binmode($fh, $opt->{"BINMODE"})
!ll&&!rl&&r
2021
MC/DC100%2/2
defined $opt->{"BINMODE"} and not binmode($fh, $opt->{"BINMODE"})
defined $opt->{"BINMODE"} not binmode($fh, $opt->{"BINMODE"})
Truth table100%3/3
A and not(B)
A= defined $opt->{"BINMODE"}B= not binmode($fh, $opt->{"BINMODE"})
ABresult
0X0
100
111
45 1 _discard_atomic($fh);
46 1 confess 'binmode('.$opt->{BINMODE}.') failed';
47 }
48  
49 # locking if requested
50 22 ▶ if ($opt->{'LOCK_SH'} or $opt->{'LOCK_EX'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"LOCK_SH"} or $opt->{"LOCK_EX"}) 9 13 22
Condition100%3/3
$opt->{"LOCK_SH"} or $opt->{"LOCK_EX"}
l!l&&r!l&&!r
3613
MC/DC100%2/2
$opt->{"LOCK_SH"} or $opt->{"LOCK_EX"}
$opt->{"LOCK_SH"} $opt->{"LOCK_EX"}
Truth table100%3/3
A or B
A= $opt->{"LOCK_SH"}B= $opt->{"LOCK_EX"}
ABresult
1X1
011
000
51 9 ▶ if (not flock($fh,
Branch: 4 branches
BranchTrueFalseTotal
unless (flock $fh, ($opt->{"LOCK_SH"} ? 1 : 0) | ($opt->{"LOCK_EX"} ? 2 : 0) | ($opt->{"LOCK_NB"} ? 4 : 0)) 5 4 9
$opt->{'LOCK_SH'} ? : 3 6 9
$opt->{'LOCK_EX'} ? : 6 3 9
$opt->{'LOCK_NB'} ? : 6 3 9
52 ($opt->{'LOCK_SH'} ? LOCK_SH : 0)
53 | ($opt->{'LOCK_EX'} ? LOCK_EX : 0)
54 | ($opt->{'LOCK_NB'} ? LOCK_NB : 0)
55 )) {
56 5 my $error = $!;
57 5 _discard_atomic($fh);
58 5 confess 'flock failed - '.$error;
59 }
60 }
61  
62 17 return $fh;
63 }
64 1 ▶ if ($type eq 'iofile') { return $proper_what }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "iofile") 1 0 1
65 0 ▶ if ($type eq 'iostring') { return $proper_what }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "iostring") 0 0 0
66 0 ▶ if ($type eq 'http') { die 'no http support yet :-|' }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "http") 0 0 0
67 }
68  
69 39 ▶sub _guess_what {
Subroutine _guess_what: called CC=18 SCAR=29.5
70 39 my $class = shift;
71 39 my $what = shift;
72  
73 39 ▶ if (!blessed($what)) { } # not blessed, do nothing
Branch: 4 branches
BranchTrueFalseTotal
if (not builtin::blessed($what)) { } 38 1 39
elsif ($what->isa('Path::Class::File')) { } 0 1 1
elsif (&any(sub { $what->isa($_); } , 'IO::File', 'IO::AtomicFile', 'IO::Uncompress::Bunzip2')) { } 1 0 1
elsif ($what->isa('IO::String')) { } 0 0 0
74 0 elsif ($what->isa('Path::Class::File')) { $what = $what->stringify }
75 1 ▶ elsif (any { $what->isa($_) } qw(IO::File IO::AtomicFile IO::Uncompress::Bunzip2)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 ▶ confess 'passed unopened IO::File'
Branch: 1 branch
BranchTrueFalseTotal
unless $what->opened 0 1 1
77 if not $what->opened;
78 1 return ('iofile', $what);
79 }
80 0 elsif ($what->isa('IO::String')) { return ('iostring', $what) }
81 0 else { confess 'no support for '.blessed($what) };
82  
83 38 my $ref_what = ref($what);
84 38 ▶ if ($ref_what eq 'ARRAY') { return ('file', File::Spec->catfile(@{$what})) }
Branch: 3 branches
BranchTrueFalseTotal
if ($ref_what eq 'ARRAY') { } 23 15 38
elsif ($ref_what eq 'SCALAR') { } 1 14 15
elsif ($ref_what eq '') { } 14 0 14
85 1 elsif ($ref_what eq 'SCALAR') { return ('string', $what) }
86 elsif ($ref_what eq '') {} # do nothing here if not reference
87 0 else { confess 'no support for ref '.(ref $what) }
88  
89 # check for typeglobs
90 14 ▶ if ((ref \$what eq 'GLOB') and (my $fh = *{$what}{IO})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$what eq "GLOB" and my $fh = *{$what;}{"IO"}) 1 13 14
Condition100%2/2
ref \$what eq "GLOB" and my $fh = *{$what;}{"IO"}
!ll
131
MC/DC100%1/1
ref \$what eq "GLOB" and my $fh = *{$what;}{"IO"}
ref \$what eq "GLOB"
Truth table100%2/2
A
A= ref \$what eq "GLOB"
Aresult
00
11
91 1 return ('iofile', $fh);
92 }
93  
94 13 ▶ if ($what =~ m{^file://(.+)$}) { return ('file', $1) } # local file
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ m[^file://(.+)$]) 1 12 13
95 12 ▶ if ($what =~ m{^https?://}) { return ('http', $what) } # http link
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ m[^https?://]) 2 10 12
96 10 ▶ if ($what =~ m{^<}) { return ('string', $what) } # xml string
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^</) 1 9 10
97 9 ▶ if ($what =~ m(^{)) { return ('string', $what) } # json string
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^{/) 2 7 9
98 7 ▶ if ($what =~ m{^\[}) { return ('string', $what) } # json string
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^\[/) 1 6 7
99 6 ▶ if ($what =~ m{\n[\s\w]}) { return ('string', $what) } # multi-line string
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /\n[\s\w]/) 1 5 6
100 5 ▶ if ($what eq '') { return ('string', '') } # empty string
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "") 2 3 5
101 3 { return ('file', $what) } # default is filename
102 }
103  
104 12 ▶sub read {
Subroutine read: called CC=2 SCAR=7.0
Pod documented
105 12 my $class = shift;
106 12 my $what = shift;
107 12 my $opt = shift;
108 12 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 12 12
109 if @_;
110  
111 12 return $class->new($what, '<', $opt);
112 }
113  
114 2 ▶sub write {
Subroutine write: called CC=2 SCAR=7.0
Pod documented
115 2 my $class = shift;
116 2 my $what = shift;
117 2 my $opt = shift;
118 2 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
119 if @_;
120  
121 2 return $class->new($what, '>', $opt);
122 }
123  
124 6 ▶sub slurp {
Subroutine slurp: called CC=2 SCAR=6.9
Pod documented
125 6 my $class = shift;
126 6 my $what = shift;
127 6 my $opt = shift;
128 6 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
129 if @_;
130  
131 6 my $fh = $class->read($what, $opt);
132 6 my $content = do { local $/; <$fh> };
133 6 close $fh;
134 6 return $content;
135 }
136  
137 11 ▶sub spew {
Subroutine spew: called CC=4 SCAR=14.3
Pod documented
138 11 my $class = shift;
139 11 my $what = shift;
140 11 my $data = shift;
141 11 my $opt = shift;
142 11 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 11 11
143 if @_;
144  
145 # "parade" to allow safe locking
146 11 my $fh = $class->new($what, '+>>', $opt);
147 8 $fh->seek(0,0);
148 8 $fh->truncate(0);
149  
150 8 ▶ if (not print $fh $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $fh $data) 0 8 8
151 0 my $error = $!;
152 0 _discard_atomic($fh);
153 0 confess 'failed to write file - '.$error;
154 }
155 8 ▶ if (not $fh->close) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->close) 1 7 8
156 1 my $error = $!;
157 1 _discard_atomic($fh);
158 1 confess 'failed to close file - '.$error;
159 }
160 7 return;
161 }
162  
163 # remove the temporary file of a failed IO::AtomicFile write, the target stays intact
164 7 ▶sub _discard_atomic {
Subroutine _discard_atomic: called CC=5 SCAR=16.2
165 7 my $fh = shift;
166 7 ▶ return if not blessed($fh) or not $fh->isa('IO::AtomicFile');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($fh) and $fh->isa("IO::AtomicFile") 4 3 7
Condition66%2/3
builtin::blessed($fh) and $fh->isa("IO::AtomicFile")
!ll&&!rl&&r
043
MC/DC50%1/2
builtin::blessed($fh) and $fh->isa("IO::AtomicFile")
builtin::blessed($fh) $fh->isa("IO::AtomicFile")
Truth table66%2/3
A and B
A= builtin::blessed($fh)B= $fh->isa("IO::AtomicFile")
ABresult
0X0
100
111
167  
168 # delete() is a no-op once close() has run (also a failed one)
169 3 my $temp = ${*$fh}{'io_atomicfile_temp'};
170 3 $fh->delete;
171 3 ▶ unlink($temp) if defined $temp and -e $temp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $temp and -e $temp 1 2 3
Condition66%2/3
defined $temp and -e $temp
!ll&&!rl&&r
021
MC/DC50%1/2
defined $temp and -e $temp
defined $temp -e $temp
Truth table66%2/3
A and B
A= defined $tempB= -e $temp
ABresult
0X0
100
111
172 3 return;
173 }
174  
175 2 1;
176  
177  
178 __END__
↑ Summary