blib/lib/IO/Callback.pm

filter: Statementstmt 97.0% 231 / 238 Branchbran 94.8% 146 / 154 Conditioncond 94.1% 48 / 51 MC/DCmcdc 89.6% 26 / 29 Subroutinesub 96.6% 29 / 30 Podpod 5.0% 1 / 20 totaltotal 92.1% 481 / 522 SCAR 45.8
CC
97
Cov
96%
getline
26.0
write
19.0
read
16.0
CRAP 97.6
↑ Summary Build »
1 package IO::Callback;
2  
3 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 IO::Callback - Emulate file interface for a code reference
9  
10 =head1 VERSION
11  
12 Version 1.12
13  
14 =cut
15  
16 23 our $VERSION = '1.12';
17  
18 =head1 SYNOPSIS
19  
20 C<IO::Callback> provides an easy way to produce a phoney read-only filehandle that calls back to your own code when it needs data to satisfy a read. This is useful if you want to use a library module that expects to read data from a filehandle, but you want the data to come from some other source and you don't want to read it all into memory and use L<IO::String>.
21  
22 use IO::Callback;
23  
24 my $fh = IO::Callback->new('<', sub { ... ; return $data });
25 my $object = Some::Class->new_from_file($fh);
26  
27 Similarly, IO::Callback allows you to wrap up a coderef as a write-only filehandle, which you can pass to a library module that expects to write its output to a filehandle.
28  
29 my $fh = IO::Callback->new('>', sub { my $data = shift ; ... });
30 $object->dump_to_file($fh);
31  
32  
33 =head1 CONSTRUCTOR
34  
35 =head2 C<new ( MODE, CODEREF [,ARG ...] )>
36  
37 Returns a filehandle object encapsulating the coderef.
38  
39 MODE must be either C<E<lt>> for a read-only filehandle or C<E<gt>> for a write-only filehandle.
40  
41 For a read-only filehandle, the callback coderef will be invoked in a scalar context each time more data is required to satisfy a read. It must return some more input data (at least one byte) as a string. If there is no more data to be read, then the callback should return either C<undef> or the empty string. If ARG values were supplied to the constructor, then they will be passed to the callback each time it is invoked.
42  
43 For a write-only filehandle, the callback will be invoked each time there is data to be written. The first argument will be the data as a string, which will always be at least one byte long. If ARG values were supplied to the constructor, then they will be passed as additional arguments to the callback. When the filehandle is closed, the callback will be invoked once with the empty string as its first argument.
44  
45 To simulate a non-fatal error on the file, the callback should set C<$!> and return the special value C<IO::Callback::Error>. See examples 6 and 7 below.
46  
47 =head1 EXAMPLES
48  
49 =over 4
50  
51 =item Example 1
52  
53 To generate a filehandle from which an infinite number of C<x> characters can be read:
54  
55 =for test "ex1" begin
56  
57 my $fh = IO::Callback->new('<', sub {"xxxxxxxxxxxxxxxxxxxxxxxxxxx"});
58  
59 my $x = $fh->getc; # $x now contains "x"
60 read $fh, $x, 5; # $x now contains "xxxxx"
61  
62 =for test "ex1" end
63  
64 =item Example 2
65  
66 A filehandle from which 1000 C<foo> lines can be read before EOF:
67  
68 =for test "ex2" begin
69  
70 my $count = 0;
71 my $fh = IO::Callback->new('<', sub {
72 return if ++$count > 1000; # EOF
73 return "foo\n";
74 });
75  
76 my $x = <$fh>; # $x now contains "foo\n"
77 read $fh, $x, 2; # $x now contains "fo"
78 read $fh, $x, 2; # $x now contains "o\n"
79 read $fh, $x, 20; # $x now contains "foo\nfoo\nfoo\nfoo\nfoo\n"
80 my @foos = <$fh>; # @foos now contains ("foo\n") x 993
81  
82 =for test "ex2" end
83  
84 The example above uses a C<closure> (a special kind of anonymous sub, see L<http://perldoc.perl.org/perlfaq7.html#What's-a-closure?>) to allow the callback to keep track of how many lines it has returned. You don't have to use a closure if you don't want to, since C<IO::Callback> will forward extra constructor arguments to the callback. This example could be re-written as:
85  
86 =for test "ex2a" begin
87  
88 my $count = 0;
89 my $fh = IO::Callback->new('<', \&my_callback, \$count);
90  
91 my $x = <$fh>; # $x now contains "foo\n"
92 read $fh, $x, 2; # $x now contains "fo"
93 read $fh, $x, 2; # $x now contains "o\n"
94 read $fh, $x, 20; # $x now contains "foo\nfoo\nfoo\nfoo\nfoo\n"
95 my @foos = <$fh>; # @foos now contains ("foo\n") x 993
96  
97 sub my_callback {
98 my $count_ref = shift;
99  
100 return if ++$$count_ref > 1000; # EOF
101 return "foo\n";
102 };
103  
104 =for test "ex2a" end
105  
106 =item Example 3
107  
108 To generate a filehandle interface to data drawn from an SQL table:
109  
110 =for test "ex3" begin
111  
112 my $sth = $dbh->prepare("SELECT ...");
113 $sth->execute;
114 my $fh = IO::Callback->new('<', sub {
115 my @row = $sth->fetchrow_array;
116 return unless @row; # EOF
117 return join(',', @row) . "\n";
118 });
119  
120 # ...
121  
122 =for test "ex3" end
123  
124 =item Example 4
125  
126 You want a filehandle to which data can be written, where the data is discarded but an exception is raised if the data includes the string C<foo>.
127  
128 =for test "ex4" begin
129  
130 my $buf = '';
131 my $fh = IO::Callback->new('>', sub {
132 $buf .= shift;
133 die "foo written" if $buf =~ /foo/;
134  
135 if ($buf =~ /(fo?)\z/) {
136 # Part way through a "foo", carry over to the next block.
137 $buf = $1;
138 } else {
139 $buf = '';
140 }
141 });
142  
143 =for test "ex4" end
144  
145 =item Example 5
146  
147 You have been given an object with a copy_data_out() method that takes a destination filehandle as an argument. You don't want the data written to a file though, you want it split into 1024-byte blocks and inserted into an SQL database.
148  
149 =for test "ex5" begin
150  
151 my $blocksize = 1024;
152 my $sth = $dbh->prepare('INSERT ...');
153  
154 my $buf = '';
155 my $fh = IO::Callback->new('>', sub {
156 $buf .= shift;
157 while (length $buf >= $blocksize) {
158 $sth->execute(substr $buf, 0, $blocksize, '');
159 }
160 });
161  
162 $thing->copy_data_out($fh);
163  
164 if (length $buf) {
165 # There is a remainder of < $blocksize
166 $sth->execute($buf);
167 }
168  
169 =for test "ex5" end
170  
171 =item Example 6
172  
173 You're testing some code that reads data from a file, you want to check that it behaves as expected if it gets an IO error part way through the file.
174  
175 =for test "ex6" begin
176  
177 use IO::Callback;
178 use Errno qw/EIO/;
179  
180 my $block1 = "x" x 10240;
181 my $block2 = "y" x 10240;
182 my @blocks = ($block1, $block2);
183  
184 my $fh = IO::Callback->new('<', sub {
185 return shift @blocks if @blocks;
186 $! = EIO;
187 return IO::Callback::Error;
188 });
189  
190 # ...
191  
192 =for test "ex6" end
193  
194 =item Example 7
195  
196 You're testing some code that writes data to a file handle, you want to check that it behaves as expected if it gets a C<file system full> error after it has written the first 100k of data.
197  
198 =for test "ex7" begin
199  
200 use IO::Callback;
201 use Errno qw/ENOSPC/;
202  
203 my $wrote = 0;
204 my $fh = IO::Callback->new('>', sub {
205 $wrote += length $_[0];
206 if ($wrote > 100_000) {
207 $! = ENOSPC;
208 return IO::Callback::Error;
209 }
210 });
211  
212 # ...
213  
214 =for test "ex7" end
215  
216 =back
217  
218 =cut
219  
220 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 23 ▶use Errno qw/EBADF/;
Subroutine BEGIN: called CC=1 SCAR=0.0
222 23 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 23 ▶use base qw/IO::String/;
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 sub open
226 {
227 194000 ▶ my $self = shift;
Subroutine open: called CC=8 SCAR=20.8
Pod documented
228 194000 ▶ return $self->new(@_) unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 194000 194000
229  
230 194000 ▶ my $mode = shift or croak "mode missing in IO::Callback::new";
Branch: 1 branch
BranchTrueFalseTotal
my $mode = shift() or croak("mode missing in IO::Callback::new") 6 193994 194000
231 193994 ▶ if ($mode eq '<') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq '<') { } 135323 58671 193994
elsif ($mode eq '>') { } 58663 8 58671
232 135323 *$self->{R} = 1;
233 } elsif ($mode eq '>') {
234 58663 *$self->{W} = 1;
235 } else {
236 8 croak qq{invalid mode "$mode" in IO::Callback::new};
237 }
238  
239 193986 ▶ my $code = shift or croak "coderef missing in IO::Callback::new";
Branch: 1 branch
BranchTrueFalseTotal
my $code = shift() or croak("coderef missing in IO::Callback::new") 4 193982 193986
240 193982 ▶ ref $code eq "CODE" or croak "non-coderef second argument in IO::Callback::new";
Branch: 1 branch
BranchTrueFalseTotal
ref $code eq "CODE" or croak("non-coderef second argument in IO::Callback::new") 2 193980 193982
241  
242 193980 my $buf = '';
243 193980 *$self->{Buf} = \$buf;
244 193980 *$self->{Pos} = 0;
245 193980 *$self->{Err} = 0;
246 193980 *$self->{lno} = 0;
247  
248 193980 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 135259 58721 193980
249 135259 my @args = @_;
250 135259 ▶ *$self->{Code} = sub { $code->(@_, @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
251 } else {
252 58721 *$self->{Code} = $code;
253 }
254 }
255  
256 sub close
257 {
258 43 ▶ my $self = shift;
Subroutine close: called CC=8 SCAR=20.8
Pod undocumented
259 43 ▶ return unless defined *$self->{Code};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *$self->{"Code"} 8 35 43
260 35 ▶ return if *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Err"} 1 34 35
261 34 ▶ if (*$self->{W}) {
Branch: 1 branch
BranchTrueFalseTotal
if (*$self->{"W"}) 25 9 34
262 25 my $ret = *$self->{Code}('');
263 25 ▶ if ($ret and ref $ret eq 'IO::Callback::ErrorMarker') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret and ref $ret eq "IO::Callback::ErrorMarker") 1 24 25
Condition100%3/3
$ret and ref $ret eq "IO::Callback::ErrorMarker"
!ll&&!rl&&r
1231
MC/DC100%2/2
$ret and ref $ret eq "IO::Callback::ErrorMarker"
$ret ref $ret eq "IO::Callback::ErrorMarker"
Truth table100%3/3
A and B
A= $retB= ref $ret eq "IO::Callback::ErrorMarker"
ABresult
0X0
100
111
264 1 *$self->{Err} = 1;
265 1 return;
266 }
267 }
268 33 foreach my $key (qw/Code Buf Eof R W Pos lno/) {
269 231 delete *$self->{$key};
270 }
271 33 *$self->{Err} = -1;
272 33 ▶ undef *$self if $] eq "5.008"; # cargo culted from IO::String
Branch: 1 branch
BranchTrueFalseTotal
if $] eq "5.008" 0 33 33
273 33 return 1;
274 }
275  
276 sub opened
277 {
278 36 ▶ my $self = shift;
Subroutine opened: called CC=2 SCAR=6.9
Pod undocumented
279 36 ▶ return defined *$self->{R} || defined *$self->{W};
Condition100%3/3
defined *$self->{'R'} || defined *$self->{'W'}
l!l&&r!l&&!r
17172
MC/DC100%2/2
defined *$self->{'R'} || defined *$self->{'W'}
defined *$self->{'R'} defined *$self->{'W'}
Truth table100%3/3
A || B
A= defined *$self->{'R'}B= defined *$self->{'W'}
ABresult
1X1
011
000
280 }
281  
282 sub getc
283 {
284 42689 ▶ my $self = shift;
Subroutine getc: called CC=3 SCAR=11.0
Pod undocumented
285 42689 ▶ *$self->{R} or return $self->_ebadf;
Branch: 1 branch
BranchTrueFalseTotal
*$self->{"R"} or return $self->_ebadf 4 42685 42689
286 42685 my $buf;
287 42685 ▶ return $buf if $self->read($buf, 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->read($buf, 1) 39392 3293 42685
288 3293 return undef;
289 }
290  
291 sub ungetc
292 {
293 40232 ▶ my ($self, $char) = @_;
Subroutine ungetc: called CC=2 SCAR=6.9
Pod undocumented
294 40232 ▶ *$self->{R} or return $self->_ebadf;
Branch: 1 branch
BranchTrueFalseTotal
*$self->{"R"} or return $self->_ebadf 1 40231 40232
295 40231 my $buf = *$self->{Buf};
296 40231 $$buf = chr($char) . $$buf;
297 40231 --*$self->{Pos};
298 40231 delete *$self->{Eof};
299 40231 return 1;
300 }
301  
302 sub eof
303 {
304 2 ▶ my $self = shift;
Subroutine eof: called CC=1 SCAR=0.0
Pod undocumented
305 2 return *$self->{Eof};
306 }
307  
308 # Use something very distinctive for the error return code, since write callbacks
309 # may pay no attention to what they are returning, and it would be bad to mistake
310 # returned noise for an error indication.
311 sub Error () {
312 21 ▶ return bless {}, 'IO::Callback::ErrorMarker';
Subroutine Error: called CC=1 SCAR=0.0
Pod undocumented
313 }
314  
315 sub _doread {
316 1224455 ▶ my $self = shift;
Subroutine _doread: called CC=6 SCAR=17.9
317  
318 1224455 ▶ return unless *$self->{Code};
Branch: 1 branch
BranchTrueFalseTotal
unless *$self->{"Code"} 7160 1217295 1224455
319 1217295 my $newbit = *$self->{Code}();
320 1217295 ▶ if (defined $newbit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newbit) 1084819 132476 1217295
321 1084819 ▶ if (ref $newbit) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $newbit) 9 1084810 1084819
322 9 ▶ if (ref $newbit eq 'IO::Callback::ErrorMarker') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $newbit eq 'IO::Callback::ErrorMarker') { } 8 1 9
323 8 *$self->{Err} = 1;
324 8 return;
325 } else {
326 1 confess "unexpected reference type ".ref($newbit)." returned by callback";
327 }
328 }
329 1084810 ▶ if (length $newbit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $newbit) 1083809 1001 1084810
330 1083809 ${*$self->{Buf}} .= $newbit;
331 1083809 return 1;
332 }
333 }
334  
335 # fall-through for both undef and ''
336 133477 delete *$self->{Code};
337 133477 return;
338 }
339  
340 sub getline
341 {
342 201231 ▶ my $self = shift;
Subroutine getline: called CC=26 SCAR=32.6
Pod undocumented
343  
344 201231 ▶ *$self->{R} or return $self->_ebadf;
Branch: 1 branch
BranchTrueFalseTotal
*$self->{"R"} or return $self->_ebadf 2 201229 201231
345 201229 ▶ return if *$self->{Eof} || *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Eof"} or *$self->{"Err"} 67772 133457 201229
Condition100%3/3
*$self->{"Eof"} or *$self->{"Err"}
l!l&&r!l&&!r
6775418133457
MC/DC100%2/2
*$self->{"Eof"} or *$self->{"Err"}
*$self->{"Eof"} *$self->{"Err"}
Truth table100%3/3
A or B
A= *$self->{"Eof"}B= *$self->{"Err"}
ABresult
1X1
011
000
346 133457 my $buf = *$self->{Buf};
347 133457 $. = *$self->{lno};
348  
349 133457 ▶ unless (defined $/) { # slurp
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $/) 26964 106493 133457
350 26964 ▶ 1 while $self->_doread;
Branch: 1 branch
BranchTrueFalseTotal
$self->_doread and do { '???' } 192074 26964 219038
351 26964 ▶ return if *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Err"} 2 26962 26964
352 26962 *$self->{Pos} += length $$buf;
353 26962 *$self->{Eof} = 1;
354 26962 *$self->{Buf} = \(my $newbuf = '');
355 26962 $. = ++ *$self->{lno};
356 26962 return $$buf;
357 }
358  
359 106493 ▶ my $rs = length $/ ? $/ : "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
length $/ ? : 78584 27909 106493
360 106493 for (;;) {
361 # In paragraph mode, discard extra newlines.
362 647014 ▶ if ($/ eq '' and $$buf =~ s/^(\n+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($/ eq "" and $$buf =~ s/^(\n+)//) 13312 633702 647014
Condition100%3/3
$/ eq "" and $$buf =~ s/^(\n+)//
!ll&&!rl&&r
41526121844113312
MC/DC100%2/2
$/ eq "" and $$buf =~ s/^(\n+)//
$/ eq "" $$buf =~ s/^(\n+)//
Truth table100%3/3
A and B
A= $/ eq ""B= $$buf =~ s/^(\n+)//
ABresult
0X0
100
111
363 13312 *$self->{Pos} += length $1;
364 }
365 647014 my $pos = index $$buf, $rs;
366 647014 ▶ if ($pos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) 53539 593475 647014
367 53539 *$self->{Pos} += $pos+length($rs);
368 53539 my $ret = substr $$buf, 0, $pos+length($rs), '';
369 53539 ▶ unless (length $/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $/) 8226 45313 53539
370 # paragraph mode, discard extra trailing newlines
371 8226 ▶ $$buf =~ s/^(\n+)// and *$self->{Pos} += length $1;
Branch: 1 branch
BranchTrueFalseTotal
$$buf =~ s/^(\n+)// and *$self->{"Pos"} += length $1 2578 5648 8226
372 8226 ▶ while (*$self->{Code} and length $$buf == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*$self->{"Code"} and length $$buf == 0) 8698 8226 16924
Condition100%3/3
*$self->{"Code"} and length $$buf == 0
!ll&&!rl&&r
674214848698
MC/DC100%2/2
*$self->{"Code"} and length $$buf == 0
*$self->{"Code"} length $$buf == 0
Truth table100%3/3
A and B
A= *$self->{"Code"}B= length $$buf == 0
ABresult
0X0
100
111
373 8698 $self->_doread;
374 8698 ▶ return if *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Err"} 0 8698 8698
375 8698 ▶ $$buf =~ s/^(\n+)// and *$self->{Pos} += length $1;
Branch: 1 branch
BranchTrueFalseTotal
$$buf =~ s/^(\n+)// and *$self->{"Pos"} += length $1 1364 7334 8698
376 }
377 }
378 53539 ▶ $self->_doread while *$self->{Code} and length $$buf == 0 and not *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
*$self->{"Code"} and length $$buf == 0 and not *$self->{"Err"} and do { $self->_doread } 23183 53539 76722
Condition83%5/6
*$self->{"Code"} and length $$buf == 0 and not *$self->{"Err"}
!ll&&!rl&&r
53539023183
*$self->{"Code"} and length $$buf == 0
!ll&&!rl&&r
198453369423183
MC/DC66%2/3
*$self->{"Code"} and length $$buf == 0 and not *$self->{"Err"}
*$self->{"Code"} length $$buf == 0 not *$self->{"Err"}
Truth table75%3/4
A and B and not(C)
A= *$self->{"Code"}B= length $$buf == 0C= not *$self->{"Err"}
ABCresult
0XX0
10X0
1100
1111
379 53539 ▶ if (length $$buf == 0 and not *$self->{Code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$buf == 0 and not *$self->{"Code"}) 19845 33694 53539
Condition66%2/3
length $$buf == 0 and not *$self->{"Code"}
!ll&&!rl&&r
33694019845
MC/DC50%1/2
length $$buf == 0 and not *$self->{"Code"}
length $$buf == 0 not *$self->{"Code"}
Truth table66%2/3
A and not(B)
A= length $$buf == 0B= not *$self->{"Code"}
ABresult
0X0
100
111
380 19845 *$self->{Eof} = 1;
381 }
382 53539 $. = ++ *$self->{lno};
383 53539 return $ret;
384 }
385 593475 ▶ if (*$self->{Code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (*$self->{'Code'}) { } 540525 52950 593475
386 540525 $self->_doread;
387 540525 ▶ return if *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Err"} 4 540521 540525
388 } else {
389 # EOL not in buffer and no more data to come - the last line is missing its EOL.
390 52950 *$self->{Eof} = 1;
391 52950 *$self->{Pos} += length $$buf;
392 52950 *$self->{Buf} = \(my $newbuf = '');
393 52950 ▶ $. = ++ *$self->{lno} if length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if length $$buf 42490 10460 52950
394 52950 ▶ return $$buf if length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if length $$buf 42490 10460 52950
395 10460 return;
396 }
397 }
398 }
399  
400 sub getlines
401 {
402 60753 ▶ croak "getlines() called in scalar context" unless wantarray;
Subroutine getlines: called CC=8 SCAR=20.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 60752 60753
403 60752 my $self = shift;
404  
405 60752 ▶ *$self->{R} or return $self->_ebadf;
Branch: 1 branch
BranchTrueFalseTotal
*$self->{"R"} or return $self->_ebadf 2 60750 60752
406 60750 ▶ return if *$self->{Err} || *$self->{Eof};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Err"} or *$self->{"Eof"} 3859 56891 60750
Condition100%3/3
*$self->{"Err"} or *$self->{"Eof"}
l!l&&r!l&&!r
18384156891
MC/DC100%2/2
*$self->{"Err"} or *$self->{"Eof"}
*$self->{"Err"} *$self->{"Eof"}
Truth table100%3/3
A or B
A= *$self->{"Err"}B= *$self->{"Eof"}
ABresult
1X1
011
000
407  
408 # To exactly match Perl's behavior on real files, getlines() should not
409 # increment $. if there is no more input, but getline() should. I won't
410 # call getline() until I've established that there is more input.
411 56891 my $buf = *$self->{Buf};
412 56891 ▶ unless (length $$buf) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $$buf) 41563 15328 56891
413 41563 $self->_doread;
414 41563 ▶ return unless length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$buf 5920 35643 41563
415 }
416  
417 50971 my($line, @lines);
418 50971 ▶ push(@lines, $line) while defined($line = $self->getline);
Branch: 1 branch
BranchTrueFalseTotal
defined($line = $self->getline) and do { push @lines, $line } 68855 50971 119826
419 50971 return @lines;
420 }
421  
422 sub READLINE
423 {
424 71105 ▶ goto &getlines if wantarray;
Subroutine READLINE: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 30388 40717 71105
425 40717 goto &getline;
426 }
427  
428 sub read
429 {
430 332458 ▶ my $self = shift;
Subroutine read: called CC=16 SCAR=27.7
Pod undocumented
431  
432 332458 ▶ *$self->{R} or return $self->_ebadf;
Branch: 1 branch
BranchTrueFalseTotal
*$self->{"R"} or return $self->_ebadf 5 332453 332458
433 332453 ▶ my $len = $_[1]||0;
Condition100%2/2
$_[1] || 0
l!l
331652801
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
434  
435 332453 ▶ croak "Negative length" if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 400 332053 332453
436 332053 ▶ return if *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Err"} 0 332053 332053
437 332053 ▶ return 0 if *$self->{Eof};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Eof"} 5282 326771 332053
438 326771 my $buf = *$self->{Buf};
439  
440 326771 ▶ 1 while *$self->{Code} and $len > length $$buf and $self->_doread;
Branch: 1 branch
BranchTrueFalseTotal
*$self->{"Code"} and $len > length $$buf and $self->_doread and do { '???' } 344125 326770 670895
Condition100%6/6
*$self->{"Code"} and $len > length $$buf and $self->_doread
!ll&&!rl&&r
27944847323344125
*$self->{"Code"} and $len > length $$buf
!ll&&!rl&&r
36495242953391448
MC/DC100%3/3
*$self->{"Code"} and $len > length $$buf and $self->_doread
*$self->{"Code"} $len > length $$buf $self->_doread
Truth table100%4/4
A and B and C
A= *$self->{"Code"}B= $len > length $$bufC= $self->_doread
ABCresult
0XX0
10X0
1100
1111
441 326770 ▶ return if *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Err"} 2 326768 326770
442 326768 ▶ if ($len > length $$buf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $$buf) 81295 245473 326768
443 81295 $len = length $$buf;
444 81295 ▶ *$self->{Eof} = 1 unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 39618 41677 81295
445 }
446  
447 326768 ▶ if (@_ > 2) { # read offset
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) { } 2281 324487 326768
448 2281 ▶ my $offset = $_[2]||0;
Condition100%2/2
$_[2] || 0
l!l
1921360
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
449 2281 ▶ if ($offset < -1 * length $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < -1 * length($_[0])) 480 1801 2281
450 480 croak "Offset outside string";
451 }
452 1801 ▶ if ($offset > length $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > length $_[0]) 480 1321 1801
453 480 $_[0] .= "\0" x ($offset - length $_[0]);
454 }
455 1801 substr($_[0], $offset) = substr($$buf, 0, $len, '');
456 }
457 else {
458 324487 $_[0] = substr($$buf, 0, $len, '');
459 }
460 326288 *$self->{Pos} += $len;
461 326288 return $len;
462 }
463  
464 23 *sysread = \&read;
465 23 *syswrite = \&write;
466  
467 sub stat {
468 0 ▶ my $self = shift;
Subroutine stat: not called CC=3 SCAR=24.8
Pod undocumented
469 0 ▶ return unless $self->opened;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->opened 0 0 0
470 0 ▶ return 1 unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
471  
472 0 my @stat = $self->SUPER::stat();
473  
474 # size unknown, report 0
475 0 $stat[7] = 0;
476 0 $stat[12] = 1;
477  
478 0 return @stat;
479 }
480  
481 sub print
482 {
483 54454 ▶ my $self = shift;
Subroutine print: called CC=5 SCAR=16.1
Pod undocumented
484  
485 54454 my $result;
486 54454 ▶ if (defined $\) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $\) { } 40802 13652 54454
487 40802 ▶ if (defined $,) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $,) { } 30601 10201 40802
488 30601 $result = $self->write(join($,, @_).$\);
489 }
490 else {
491 10201 $result = $self->write(join("",@_).$\);
492 }
493 }
494 else {
495 13652 ▶ if (defined $,) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $,) { } 10200 3452 13652
496 10200 $result = $self->write(join($,, @_));
497 }
498 else {
499 3452 $result = $self->write(join("",@_));
500 }
501 }
502  
503 54454 ▶ return unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 30 54424 54454
504 54424 return 1;
505 }
506 23 *printflush = \*print;
507  
508 sub printf
509 {
510 3462 ▶ my $self = shift;
Subroutine printf: called CC=2 SCAR=6.9
Pod undocumented
511 3462 my $fmt = shift;
512 3462 my $result = $self->write(sprintf($fmt, @_));
513 3462 ▶ return unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 48 3414 3462
514 3414 return 1;
515 }
516  
517 sub getpos
518 {
519 773863 ▶ my $self = shift;
Subroutine getpos: called CC=1 SCAR=0.0
Pod undocumented
520  
521 773863 $. = *$self->{lno};
522 773863 return *$self->{Pos};
523 }
524 23 *tell = \&getpos;
525 23 *pos = \&getpos;
526  
527 sub setpos
528 {
529 1 ▶ croak "setpos not implemented for IO::Callback";
Subroutine setpos: called CC=1 SCAR=0.0
Pod undocumented
530 }
531  
532 sub truncate
533 {
534 1 ▶ croak "truncate not implemented for IO::Callback";
Subroutine truncate: called CC=1 SCAR=0.0
Pod undocumented
535 }
536  
537 sub seek
538 {
539 1 ▶ croak "Illegal seek";
Subroutine seek: called CC=1 SCAR=0.0
Pod undocumented
540 }
541 23 *sysseek = \&seek;
542  
543 sub write
544 {
545 58771 ▶ my $self = shift;
Subroutine write: called CC=19 SCAR=29.4
Pod undocumented
546  
547 58771 ▶ *$self->{W} or return $self->_ebadf;
Branch: 1 branch
BranchTrueFalseTotal
*$self->{"W"} or return $self->_ebadf 23 58748 58771
548 58748 ▶ return if *$self->{Err};
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Err"} 81 58667 58748
549  
550 58667 my $slen = length($_[0]);
551 58667 my $len = $slen;
552 58667 my $off = 0;
553 58667 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 815 57852 58667
554 815 ▶ my $xlen = defined $_[1] ? $_[1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 739 76 815
555 815 ▶ $len = $xlen if $xlen < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $xlen < $len 390 425 815
556 815 ▶ croak "Negative length" if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 80 735 815
557 735 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 592 143 735
558 592 ▶ $off = $_[2] || 0;
Condition100%2/2
$_[2] || 0
l!l
49696
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
559 592 ▶ if ( $off >= $slen and $off > 0 and ($] < 5.011 or $off > $slen) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off >= $slen and $off > 0 and $] < "5.011" || $off > $slen) 120 472 592
Condition88%8/9
$off >= $slen and $off > 0 and $] < "5.011" || $off > $slen
!ll&&!rl&&r
43636120
$off >= $slen and $off > 0
!ll&&!rl&&r
39640156
$] < "5.011" or $off > $slen
l!l&&r!l&&!r
012036
MC/DC75%3/4
$off >= $slen and $off > 0 and $] < "5.011" || $off > $slen
$off >= $slen $off > 0 $] < "5.011" $off > $slen
Truth table80%4/5
A and B and C or D
A= $off >= $slenB= $off > 0C= $] < "5.011"D= $off > $slen
ABCDresult
0XXX0
10XX0
11000
111X1
11011
560 120 croak "Offset outside string";
561 }
562 472 ▶ if ($off < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < 0) 237 235 472
563 237 $off += $slen;
564 237 ▶ croak "Offset outside string" if $off < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 120 117 237
565 }
566 352 my $rem = $slen - $off;
567 352 ▶ $len = $rem if $rem < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $rem < $len 58 294 352
568 }
569 }
570 58347 ▶ return $len if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 9769 48578 58347
571 48578 my $ret = *$self->{Code}(substr $_[0], $off, $len);
572 48578 ▶ if (defined $ret and ref $ret eq 'IO::Callback::ErrorMarker') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret and ref $ret eq "IO::Callback::ErrorMarker") 12 48566 48578
Condition100%3/3
defined $ret and ref $ret eq "IO::Callback::ErrorMarker"
!ll&&!rl&&r
54856112
MC/DC100%2/2
defined $ret and ref $ret eq "IO::Callback::ErrorMarker"
defined $ret ref $ret eq "IO::Callback::ErrorMarker"
Truth table100%3/3
A and B
A= defined $retB= ref $ret eq "IO::Callback::ErrorMarker"
ABresult
0X0
100
111
573 12 *$self->{Err} = 1;
574 12 return;
575 }
576 48566 *$self->{Pos} += $len;
577 48566 return $len;
578 }
579  
580 sub error {
581 2885 ▶ my $self = shift;
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
582  
583 2885 return *$self->{Err};
584 }
585  
586 sub clearerr {
587 33 ▶ my $self = shift;
Subroutine clearerr: called CC=1 SCAR=0.0
Pod undocumented
588  
589 33 *$self->{Err} = 0;
590 }
591  
592 sub _ebadf {
593 37 ▶ my $self = shift;
Subroutine _ebadf: called CC=1 SCAR=0.0
594  
595 37 $! = EBADF;
596 37 *$self->{Err} = -1;
597 37 return;
598 }
599  
600 23 *GETC = \&getc;
601 23 *PRINT = \&print;
602 23 *PRINTF = \&printf;
603 23 *READ = \&read;
604 23 *WRITE = \&write;
605 23 *SEEK = \&seek;
606 23 *TELL = \&getpos;
607 23 *EOF = \&eof;
608 23 *CLOSE = \&close;
609  
610 =head1 AUTHOR
611  
612 Dave Taylor, C<< <dave.taylor.cpan at gmail.com> >>
613  
614 =head1 BUGS AND LIMITATIONS
615  
616 Fails to inter-operate with some library modules that read or write filehandles from within XS code. I am aware of the following specific cases, please let me know if you run into any others:
617  
618 =over 4
619  
620 =item C<Digest::MD5::addfile()>
621  
622 =back
623  
624 Please report any other bugs or feature requests to C<bug- at rt.cpan.org>, or through
625 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO::Callback>. I will be notified, and then you'll
626 automatically be notified of progress on your bug as I make changes.
627  
628 =head1 SUPPORT
629  
630 You can find documentation for this module with the perldoc command.
631  
632 perldoc IO::Callback
633  
634 You can also look for information at:
635  
636 =over 4
637  
638 =item * RT: CPAN's request tracker
639  
640 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO::Callback>
641  
642 =item * AnnoCPAN: Annotated CPAN documentation
643  
644 L<http://annocpan.org/dist/IO::Callback>
645  
646 =item * CPAN Ratings
647  
648 L<http://cpanratings.perl.org/d/IO::Callback>
649  
650 =item * Search CPAN
651  
652 L<http://search.cpan.org/dist/IO::Callback>
653  
654 =back
655  
656 =head1 SEE ALSO
657  
658 L<IO::String>, L<IO::Stringy>, L<perlfunc/open>
659  
660 =head1 ACKNOWLEDGEMENTS
661  
662 Adapted from code in L<IO::String> by Gisle Aas.
663  
664 =head1 MANITAINER
665  
666 This module is currently being maintained by Toby Inkster (TOBYINK)
667 for bug fixes. No substantial changes or new features are planned.
668  
669 =head1 COPYRIGHT & LICENSE
670  
671 Copyright 1998-2005 Gisle Aas.
672  
673 Copyright 2009-2010 Dave Taylor.
674  
675 This program is free software; you can redistribute it and/or modify it
676 under the same terms as Perl itself.
677  
678 =cut
679  
680 23 1; # End of IO::Callback
↑ Summary Build »