blib/lib/IO/Any.pm

filter: Statementstmt 78.2% 119 / 152 Branchbran 69.5% 57 / 82 Conditioncond 61.1% 11 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 79.1% 19 / 24 Podpod 100% 5 / 5 totaltotal 73.8% 215 / 291 CC 50 SCAR 26.6
CC
50
Cov
74%
new
24.7
_guess_what
19.0
slurp
13.1
CRAP 14.3
↑ 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.09';
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::MoreUtils qw(none any);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 23 ▶sub new {
Subroutine new: called CC=21 SCAR=32.1
Pod documented
19 23 my $class = shift;
20 23 my $what = shift;
21 23 ▶ my $how = shift || '<';
Condition50%1/2
shift() || '<'
l!l
230
MC/DC0%0/1
shift() || '<'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
22 23 ▶ my $opt = shift || {};
Condition100%2/2
shift() || {}
l!l
1211
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
23 23 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 23 23
24 if @_;
25  
26 23 ▶ confess '$what is missing'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $what 2 21 23
27 if not defined $what;
28  
29 21 ▶ confess 'expecting hash ref'
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt ne "HASH" 0 21 21
30 if ref $opt ne 'HASH';
31 21 foreach my $key (keys %$opt) {
32 confess 'unknown option '.$key
33 17 ▶ if (none { $key eq $_ } qw(atomic LOCK_SH LOCK_EX LOCK_NB));
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $key eq $_; } , "atomic", "LOCK_SH", "LOCK_EX", "LOCK_NB") 2 15 17
34 }
35  
36 19 my ($type, $proper_what) = $class->_guess_what($what);
37  
38  
39 19 ▶ if ($type eq 'string') { return IO::String->new($proper_what) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string") 3 16 19
40 16 ▶ if ($type eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "file") 15 1 16
41 15 ▶ my $fh = $opt->{'atomic'} ? IO::AtomicFile->new() : IO::File->new();
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'atomic'} ? : 2 13 15
42 15 ▶ $fh->open($proper_what, $how)
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($proper_what, $how) or confess('error opening file "' . $proper_what . '" - ' . $!) 15 0 15
43 or confess 'error opening file "'.$proper_what.'" - '.$!;
44  
45 # locking if requested
46 15 ▶ if ($opt->{'LOCK_SH'} or $opt->{'LOCK_EX'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"LOCK_SH"} or $opt->{"LOCK_EX"}) 8 7 15
Condition100%3/3
$opt->{"LOCK_SH"} or $opt->{"LOCK_EX"}
l!l&&r!l&&!r
357
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
47 flock($fh,
48 ($opt->{'LOCK_SH'} ? LOCK_SH : 0)
49 | ($opt->{'LOCK_EX'} ? LOCK_EX : 0)
50 8 ▶ | ($opt->{'LOCK_NB'} ? LOCK_NB : 0)
Branch: 4 branches
BranchTrueFalseTotal
flock $fh, ($opt->{"LOCK_SH"} ? 1 : 0) | ($opt->{"LOCK_EX"} ? 2 : 0) | ($opt->{"LOCK_NB"} ? 4 : 0) or confess('flock failed - ' . $!) 4 4 8
$opt->{'LOCK_SH'} ? : 3 5 8
$opt->{'LOCK_EX'} ? : 5 3 8
$opt->{'LOCK_NB'} ? : 5 3 8
51 ) or confess 'flock failed - '.$!;
52 }
53  
54 11 return $fh;
55 }
56 1 ▶ if ($type eq 'iofile') { return $proper_what }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "iofile") 1 0 1
57 0 ▶ if ($type eq 'iostring') { return $proper_what }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "iostring") 0 0 0
58 0 ▶ if ($type eq 'http') { die 'no http support yet :-|' }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "http") 0 0 0
59 }
60  
61 31 ▶sub _guess_what {
Subroutine _guess_what: called CC=18 SCAR=29.5
62 31 my $class = shift;
63 31 my $what = shift;
64  
65 31 ▶ if (!blessed($what)) { } # not blessed, do nothing
Branch: 4 branches
BranchTrueFalseTotal
if (not builtin::blessed($what)) { } 30 1 31
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
66 0 elsif ($what->isa('Path::Class::File')) { $what = $what->stringify }
67 1 ▶ elsif (any { $what->isa($_) } qw(IO::File IO::AtomicFile IO::Uncompress::Bunzip2)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 ▶ confess 'passed unopened IO::File'
Branch: 1 branch
BranchTrueFalseTotal
unless $what->opened 0 1 1
69 if not $what->opened;
70 1 return ('iofile', $what);
71 }
72 0 elsif ($what->isa('IO::String')) { return ('iostring', $what) }
73 0 else { confess 'no support for '.blessed($what) };
74  
75 30 my $ref_what = ref($what);
76 30 ▶ if ($ref_what eq 'ARRAY') { return ('file', File::Spec->catfile(@{$what})) }
Branch: 3 branches
BranchTrueFalseTotal
if ($ref_what eq 'ARRAY') { } 16 14 30
elsif ($ref_what eq 'SCALAR') { } 1 13 14
elsif ($ref_what eq '') { } 13 0 13
77 1 elsif ($ref_what eq 'SCALAR') { return ('string', $what) }
78 elsif ($ref_what eq '') {} # do nothing here if not reference
79 0 else { confess 'no support for ref '.(ref $what) }
80  
81 # check for typeglobs
82 13 ▶ 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 12 13
Condition100%2/2
ref \$what eq "GLOB" and my $fh = *{$what;}{"IO"}
!ll
121
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
83 1 return ('iofile', $fh);
84 }
85  
86 12 ▶ if ($what =~ m{^file://(.+)$}) { return ('file', $1) } # local file
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ m[^file://(.+)$]) 1 11 12
87 11 ▶ if ($what =~ m{^https?://}) { return ('http', $what) } # http link
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ m[^https?://]) 2 9 11
88 9 ▶ if ($what =~ m{^<}) { return ('string', $what) } # xml string
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^</) 1 8 9
89 8 ▶ if ($what =~ m(^{)) { return ('string', $what) } # json string
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^{/) 2 6 8
90 6 ▶ if ($what =~ m{^\[}) { return ('string', $what) } # json string
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^\[/) 1 5 6
91 5 ▶ if ($what =~ m{\n[\s\w]}) { return ('string', $what) } # multi-line string
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /\n[\s\w]/) 1 4 5
92 4 ▶ if ($what eq '') { return ('string', '') } # empty string
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "") 2 2 4
93 2 { return ('file', $what) } # default is filename
94 }
95  
96 10 ▶sub read {
Subroutine read: called CC=2 SCAR=7.0
Pod documented
97 10 my $class = shift;
98 10 my $what = shift;
99 10 my $opt = shift;
100 10 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 10 10
101 if @_;
102  
103 10 return $class->new($what, '<', $opt);
104 }
105  
106 2 ▶sub write {
Subroutine write: called CC=2 SCAR=7.0
Pod documented
107 2 my $class = shift;
108 2 my $what = shift;
109 2 my $opt = shift;
110 2 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
111 if @_;
112  
113 2 return $class->new($what, '>', $opt);
114 }
115  
116 4 ▶sub slurp {
Subroutine slurp: called CC=6 SCAR=25.8
Pod documented
117 4 my $class = shift;
118 4 my $what = shift;
119 4 my $opt = shift;
120 4 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
121 if @_;
122  
123 4 my $fh = $class->read($what, $opt);
124  
125 # use event loop when AnyEvent is loaded (skip IO::String, doesn't work and makes no sense)
126 # not supported under MSWin32
127 4 ▶ if ($INC{'AnyEvent.pm'} and not $fh->isa('IO::String') and ($^O ne 'MSWin32')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"AnyEvent.pm"} and not $fh->isa("IO::String") and $^O ne "MSWin32") 0 4 4
Condition33%2/6
$INC{"AnyEvent.pm"} and not $fh->isa("IO::String") and $^O ne "MSWin32"
!ll&&!rl&&r
400
$INC{"AnyEvent.pm"} and not $fh->isa("IO::String")
!ll&&!rl&&r
400
MC/DC0%0/3
$INC{"AnyEvent.pm"} and not $fh->isa("IO::String") and $^O ne "MSWin32"
$INC{"AnyEvent.pm"} not $fh->isa("IO::String") $^O ne "MSWin32"
Truth table25%1/4
A and not(B) and C
A= $INC{"AnyEvent.pm"}B= not $fh->isa("IO::String")C= $^O ne "MSWin32"
ABCresult
0XX0
10X0
1100
1111
128 eval 'use AnyEvent::Handle'
129 0 ▶ if not $INC{'AnyEvent/Handle.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"AnyEvent/Handle.pm"} 0 0 0
130 0 my $eof = AnyEvent->condvar;
131 0 my $content = '';
132 my $hdl = AnyEvent::Handle->new(
133 fh => $fh,
134 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 $content .= delete $_[0]->{'rbuf'};
136 },
137 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 $eof->send;
139 },
140 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 my ($hdl, $fatal, $msg) = @_;
142 0 $hdl->destroy;
143 0 $eof->croak($msg);
144 }
145 0 );
146  
147 0 $eof->recv;
148 0 $hdl->destroy;
149 0 close $fh;
150 0 return $content;
151 }
152  
153 4 my $content = do { local $/; <$fh> };
154 4 close $fh;
155 4 return $content;
156 }
157  
158 5 ▶sub spew {
Subroutine spew: called CC=6 SCAR=24.6
Pod documented
159 5 my $class = shift;
160 5 my $what = shift;
161 5 my $data = shift;
162 5 my $opt = shift;
163 5 ▶ confess 'too many arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 5 5
164 if @_;
165  
166 # "parade" to allow safe locking
167 5 my $fh = $class->new($what, '+>>', $opt);
168 4 $fh->seek(0,0);
169 4 $fh->truncate(0);
170  
171 # use event loop when AnyEvent is loaded (skip IO::String, doesn't work and makes no sense)
172 4 ▶ if ($INC{'AnyEvent.pm'} and not $fh->isa('IO::String')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"AnyEvent.pm"} and not $fh->isa("IO::String")) 0 4 4
Condition33%1/3
$INC{"AnyEvent.pm"} and not $fh->isa("IO::String")
!ll&&!rl&&r
400
MC/DC0%0/2
$INC{"AnyEvent.pm"} and not $fh->isa("IO::String")
$INC{"AnyEvent.pm"} not $fh->isa("IO::String")
Truth table33%1/3
A and not(B)
A= $INC{"AnyEvent.pm"}B= not $fh->isa("IO::String")
ABresult
0X0
100
111
173 eval 'use AnyEvent::Handle'
174 0 ▶ if not $INC{'AnyEvent/Handle.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"AnyEvent/Handle.pm"} 0 0 0
175  
176 0 my $eof = AnyEvent->condvar;
177 my $hdl = AnyEvent::Handle->new(
178 fh => $fh,
179 0 ▶ on_drain => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 $eof->send;
181 },
182 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 my ($hdl, $fatal, $msg) = @_;
184 0 $hdl->destroy;
185 0 $eof->croak($msg);
186 }
187 0 );
188  
189 0 $hdl->push_write($data);
190  
191 0 $eof->recv;
192 0 $hdl->destroy;
193 0 close $fh;
194 0 return;
195 }
196  
197 4 print $fh $data;
198 4 ▶ $fh->close || confess 'failed to close file - '.$!;
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or confess('failed to close file - ' . $!) 4 0 4
199 4 return;
200 }
201  
202 2 1;
203  
204  
205 __END__
↑ Summary