Util.pm

filter: Statementstmt 46.3% 57 / 123 Branchbran 27.2% 12 / 44 Conditioncond 21.0% 4 / 19 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 55.1% 16 / 29 Podpod 100% 17 / 17 totaltotal 44.2% 107 / 242 SCAR 54.5
CC
30
Cov
39%
is_real_fh
72.0
io_from_object
12.4
io_to_string
12.0
CRAP 231.8
↑ Summary Prototype/Fallback.pm »
1 package IO::Handle::Util;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = "0.01_01";
7 3 $VERSION = eval $VERSION;
8  
9  
10 3 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # we use this to create errors
15 #use autodie ();
16  
17 # perl blesses IO objects into these namespaces, make sure they are loaded
18 3 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use FileHandle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # fake handle types
22 #use IO::String ();
23 #use IO::Handle::Iterator ();
24  
25 #use IO::Handle::Prototype::Fallback ();
26  
27 3 use Sub::Exporter -setup => {
28 exports => [qw(
29 io_to_write_cb
30 io_to_read_cb
31 io_to_string
32 io_to_array
33 io_to_list
34 io_to_glob
35  
36 io_from_any
37 io_from_ref
38 io_from_string
39 io_from_object
40 io_from_array
41 io_from_scalar_ref
42 io_from_thunk
43 io_from_getline
44 io_from_write_cb
45 io_prototype
46  
47 is_real_fh
48 )],
49 groups => {
50 io_to => [qw(
51 io_to_write_cb
52 io_to_read_cb
53 io_to_string
54 io_to_array
55 io_to_list
56 io_to_glob
57 )],
58  
59 io_from => [qw(
60 io_from_any
61 io_from_ref
62 io_from_string
63 io_from_object
64 io_from_array
65 io_from_scalar_ref
66 io_from_thunk
67 io_from_getline
68 io_from_write_cb
69 )],
70  
71 coercion => [qw(
72 :io_to
73 :io_from
74 )],
75  
76 misc => [qw(
77 io_prototype
78 is_real_fh
79 )],
80 },
81 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 sub io_to_write_cb ($) {
84 2 ▶ my $fh = io_from_any(shift);
Subroutine io_to_write_cb: called CC=1 SCAR=0.6
Pod documented
85  
86 return sub {
87 2 ▶ local $,;
Subroutine __ANON__: called CC=2 SCAR=9.2
88 2 local $\;
89 2 ▶ $fh->print(@_) or do {
Branch: 1 branch
BranchTrueFalseTotal
$fh->print(@_) or do { my $e = $!; require autodie; die "autodie::exception"->new("function", "CORE::print", "args", [@_], "message", "\$E", "errno", $e) } 0 2 2
90 0 my $e = $!;
91 0 require autodie;
92 0 die autodie::exception->new(
93 function => q{CORE::print}, args => [@_],
94 message => "\$E", errno => $e,
95 );
96 }
97 }
98 1 }
99  
100 sub io_to_read_cb ($) {
101 0 ▶ my $fh = io_from_any(shift);
Subroutine io_to_read_cb: not called CC=1 SCAR=6.9
Pod documented
102  
103 0 ▶ return sub { scalar $fh->getline() };
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 }
105  
106 sub io_to_string ($) {
107 0 ▶ my $thing = shift;
Subroutine io_to_string: not called CC=3 SCAR=24.8
Pod documented
108  
109 0 ▶ if ( defined $thing and not ref $thing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $thing and not ref $thing) { } 0 0 0
Condition0%0/3
defined $thing and not ref $thing
!ll&&!rl&&r
000
MC/DC0%0/2
defined $thing and not ref $thing
defined $thing not ref $thing
Truth table0%0/3
A and not(B)
A= defined $thingB= not ref $thing
ABresult
0X0
100
111
110 0 return $thing;
111 } else {
112 0 my $fh = io_from_any($thing);
113  
114 # list context is in case ->getline ignores $/,
115 # which is likely the case with ::Iterator
116 0 local $/;
117 0 return join "", <$fh>;
118 }
119 }
120  
121 sub io_to_list ($) {
122 0 ▶ my $thing = shift;
Subroutine io_to_list: not called CC=3 SCAR=24.8
Pod documented
123  
124 0 ▶ warnings::warnif(__PACKAGE__, "io_to_list not invoked in list context")
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
125 unless wantarray;
126  
127 0 ▶ if ( ref $thing eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq 'ARRAY') { } 0 0 0
128 0 return @$thing;
129 } else {
130 0 my $fh = io_from_any($thing);
131 0 return <$fh>;
132 }
133 }
134  
135 sub io_to_array ($) {
136 0 ▶ my $thing = shift;
Subroutine io_to_array: not called CC=2 SCAR=17.9
Pod documented
137  
138 0 ▶ if ( ref $thing eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq 'ARRAY') { } 0 0 0
139 0 return $thing;
140 } else {
141 0 my $fh = io_from_any($thing);
142  
143 0 return [ <$fh> ];
144 }
145 }
146  
147 sub io_to_glob {
148 10 ▶ my $thing = shift;
Subroutine io_to_glob: called CC=3 SCAR=11.8
Pod documented
149  
150 10 my $fh = io_from_any($thing);
151  
152 10 ▶ if ( ref($fh) eq 'GLOB' or ref($fh) eq 'IO::Handle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq 'GLOB' or ref $fh eq 'IO::Handle') { } 0 10 10
Condition33%1/3
ref $fh eq "GLOB" or ref $fh eq "IO::Handle"
l!l&&r!l&&!r
0010
MC/DC0%0/2
ref $fh eq "GLOB" or ref $fh eq "IO::Handle"
ref $fh eq "GLOB" ref $fh eq "IO::Handle"
Truth table33%1/3
A or B
A= ref $fh eq "GLOB"B= ref $fh eq "IO::Handle"
ABresult
1X1
011
000
153 0 return $fh;
154 } else {
155 # wrap in a tied handle
156 10 my $glob = Symbol::gensym();
157  
158 10 require IO::Handle::Util::Tie;
159 10 tie *$glob, 'IO::Handle::Util::Tie', $fh;
160  
161 10 return $glob;
162 }
163 }
164  
165 sub io_from_any ($) {
166 12 ▶ my $thing = shift;
Subroutine io_from_any: called CC=2 SCAR=7.6
Pod documented
167  
168 12 ▶ if ( ref $thing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing) { } 12 0 12
169 12 return io_from_ref($thing);
170 } else {
171 0 return io_from_string($thing);
172 }
173 }
174  
175 sub io_from_ref ($) {
176 12 ▶ my $ref = shift;
Subroutine io_from_ref: called CC=7 SCAR=21.8
Pod documented
177  
178 12 ▶ if ( Scalar::Util::blessed($ref) ) {
Branch: 5 branches
BranchTrueFalseTotal
if (builtin::blessed($ref)) { } 10 2 12
elsif (ref $ref eq 'GLOB' and *{$ref;}{'IO'}) { } 1 1 2
elsif (ref $ref eq 'ARRAY') { } 0 1 1
elsif (ref $ref eq 'SCALAR') { } 1 0 1
elsif (ref $ref eq 'CODE') { } 0 0 0
179 10 return io_from_object($ref);
180 1 ▶ } elsif ( ref $ref eq 'GLOB' and *{$ref}{IO}) {
Condition100%2/2
ref $ref eq "GLOB" and *{$ref;}{"IO"}
!ll
11
MC/DC100%1/1
ref $ref eq "GLOB" and *{$ref;}{"IO"}
ref $ref eq "GLOB"
Truth table100%2/2
A
A= ref $ref eq "GLOB"
Aresult
00
11
181 # once IO::Handle is required, entersub DWIMs method invoked on globs
182 # there is no need to bless or IO::Wrap if there's a valid IO slot
183 1 return $ref;
184 } elsif ( ref $ref eq 'ARRAY' ) {
185 0 return io_from_array($ref);
186 } elsif ( ref $ref eq 'SCALAR' ) {
187 1 return io_from_scalar_ref($ref);
188 } elsif ( ref $ref eq 'CODE' ) {
189 0 Carp::croak("Coercing an IO object from a coderef is ambiguous. Please use io_from_thunk, io_from_getline or io_from_write_cb directly.");
190 } else {
191 0 Carp::croak("Don't know how to make an IO from $ref");
192 }
193 }
194  
195 sub io_from_object ($) {
196 10 ▶ my $obj = shift;
Subroutine io_from_object: called CC=5 SCAR=25.2
Pod documented
197  
198 10 ▶ if ( $obj->isa("IO::Handle") or $obj->can("getline") && $obj->can("print") ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($obj->isa('IO::Handle') or $obj->can('getline') and $obj->can('print')) { } 10 0 10
elsif ($obj->isa('Path::Class::File')) { } 0 0 0
Condition16%1/6
$obj->isa("IO::Handle") or $obj->can("getline") and $obj->can("print")
l!l&&r!l&&!r
1000
$obj->can("getline") and $obj->can("print")
!ll&&!rl&&r
000
MC/DC0%0/3
$obj->isa("IO::Handle") or $obj->can("getline") and $obj->can("print")
$obj->isa("IO::Handle") $obj->can("getline") $obj->can("print")
Truth table25%1/4
A or B and C
A= $obj->isa("IO::Handle")B= $obj->can("getline")C= $obj->can("print")
ABCresult
1XX1
0111
00X0
0100
199 10 return $obj;
200 } elsif ( $obj->isa("Path::Class::File") ) {
201 0 return $obj->openr; # safe default or open for rw?
202 } else {
203 # FIXME URI? IO::File? IO::Scalar, IO::String etc? make sure they all pass
204 0 Carp::croak("Object does not seem to be an IO::Handle lookalike");
205 }
206 }
207  
208 sub io_from_string ($) {
209 0 ▶ my $string = shift; # make sure it's a copy, IO::String will use \$_[0]
Subroutine io_from_string: not called CC=1 SCAR=6.9
Pod documented
210 0 require IO::String;
211 0 return IO::String->new($string);
212 }
213  
214 sub io_from_array ($) {
215 1 ▶ my $array = shift;
Subroutine io_from_array: called CC=1 SCAR=0.0
Pod documented
216  
217 1 my @array = @$array;
218  
219 1 require IO::Handle::Iterator;
220  
221 # IO::Lines/IO::ScalarArray is part of IO::stringy which is considered bad.
222 IO::Handle::Iterator->new(sub {
223 9 ▶ if ( @array ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (@array) { } 8 1 9
224 8 return shift @array;
225 } else {
226 1 return;
227 }
228 1 });
229 }
230  
231 sub io_from_scalar_ref ($) {
232 1 ▶ my $ref = shift;
Subroutine io_from_scalar_ref: called CC=1 SCAR=0.4
Pod documented
233 1 require IO::String;
234 0 return IO::String->new($ref);
235 }
236  
237 sub io_from_thunk ($) {
238 0 ▶ my $thunk = shift;
Subroutine io_from_thunk: not called CC=1 SCAR=6.9
Pod documented
239  
240 0 my @lines;
241  
242 0 require IO::Handle::Iterator;
243  
244 return IO::Handle::Iterator->new(sub {
245 0 ▶ if ( $thunk ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($thunk) 0 0 0
246 0 @lines = $thunk->();
247 0 undef $thunk;
248 }
249  
250 0 ▶ if ( @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) { } 0 0 0
251 0 return shift @lines;
252 } else {
253 0 return;
254 }
255 0 });
256 }
257  
258 sub io_from_getline ($) {
259 0 ▶ my $cb = shift;
Subroutine io_from_getline: not called CC=1 SCAR=6.9
Pod documented
260  
261 0 require IO::Handle::Iterator;
262  
263 0 return IO::Handle::Iterator->new($cb);
264 }
265  
266 sub io_from_write_cb ($) {
267 0 ▶ my $cb = shift;
Subroutine io_from_write_cb: not called CC=1 SCAR=6.9
Pod documented
268  
269 io_prototype( __write => sub {
270 0 ▶ local $,;
Subroutine __ANON__: not called CC=1 SCAR=6.9
271 0 local $\;
272 0 $cb->($_[1]);
273 0 } );
274 }
275  
276 sub io_prototype {
277 0 ▶ require IO::Handle::Prototype::Fallback;
Subroutine io_prototype: not called CC=1 SCAR=6.9
Pod documented
278 0 IO::Handle::Prototype::Fallback->new(@_);
279 }
280  
281 # returns true if the handle is (hopefully) suitable for passing to things that
282 # want to do non method operations on it, including operations that need a
283 # proper file descriptor
284 sub is_real_fh ($) {
285 0 ▶ my $fh = shift;
Subroutine is_real_fh: not called CC=8 SCAR=42.8
Pod documented
286  
287 0 my $reftype = Scalar::Util::reftype($fh);
288  
289 0 ▶ if ( $reftype eq 'IO'
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq 'IO' or $reftype eq 'GLOB' and *{$fh;}{'IO'}) { } 0 0 0
Condition0%0/5
$reftype eq "IO" or $reftype eq "GLOB" and *{$fh;}{"IO"}
l!l&&r!l&&!r
000
$reftype eq "GLOB" and *{$fh;}{"IO"}
!ll
00
MC/DC0%0/2
$reftype eq "IO" or $reftype eq "GLOB" and *{$fh;}{"IO"}
$reftype eq "IO" $reftype eq "GLOB"
Truth table0%0/3
A or B
A= $reftype eq "IO"B= $reftype eq "GLOB"
ABresult
1X1
011
000
290 0 or $reftype eq 'GLOB' && *{$fh}{IO}
291 ) {
292 # if it's a blessed glob make sure to not break encapsulation with
293 # fileno($fh) (e.g. if you are filtering output then file descriptor
294 # based operations might no longer be valid).
295 # then ensure that the fileno *opcode* agrees too, that there is a
296 # valid IO object inside $fh either directly or indirectly and that it
297 # corresponds to a real file descriptor.
298  
299 0 my $m_fileno = $fh->fileno;
300  
301 0 ▶ return '' unless defined $m_fileno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m_fileno 0 0 0
302 0 ▶ return '' unless $m_fileno >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $m_fileno >= 0 0 0 0
303  
304 0 my $f_fileno = fileno($fh);
305  
306 0 ▶ return '' unless defined $f_fileno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f_fileno 0 0 0
307 0 ▶ return '' unless $f_fileno >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $f_fileno >= 0 0 0 0
308  
309 0 return 1;
310 } else {
311 # anything else, including GLOBS without IO (even if they are blessed)
312 # and non GLOB objects that look like filehandle objects cannot have a
313 # valid file descriptor in fileno($fh) context so may break.
314 0 return '';
315 }
316 }
317  
318 3 __PACKAGE__
319  
320 # ex: set sw=4 et:
321  
322 __END__
↑ Summary Prototype/Fallback.pm »