Prototype/Fallback.pm

filter: Statementstmt 82.3% 126 / 153 Branchbran 78.3% 47 / 60 Conditioncond 87.5% 7 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 72.5% 29 / 40 Podpod 0.0% 0 / 1 totaltotal 79.4% 212 / 267 SCAR 37.9
CC
36
Cov
81%
__ANON__
12.0
__ANON__
9.8
__ANON__
9.0
CRAP 44.3
« Util.pm ↑ Summary Iterator.pm »
1 package IO::Handle::Prototype::Fallback;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use parent qw(IO::Handle::Prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 19 ▶ my ( $class, @args ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12  
13 19 $class->SUPER::new(
14 $class->_process_callbacks(@args),
15 );
16 }
17  
18 14 ▶sub __write { shift->_cb(__write => @_) }
Subroutine __write: called CC=1 SCAR=0.0
19 0 ▶sub __read { shift->_cb(__read => @_) }
Subroutine __read: not called CC=1 SCAR=6.9
20  
21 sub _process_callbacks {
22 34 ▶ my ( $class, %user_cb ) = @_;
Subroutine _process_callbacks: called CC=8 SCAR=20.8
23  
24 34 ▶ if ( keys %user_cb == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %user_cb == 1) 19 15 34
25 # these callbacks require wrapping of the user's callback to add
26 # buffering, so we short circuit the entire process
27 19 foreach my $fallback (qw(__read read getline)) {
28 42 ▶ if ( my $cb = $user_cb{$fallback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $user_cb{$fallback}) 15 27 42
29 15 my $method = "_default_${fallback}_callbacks";
30  
31 15 return $class->_process_callbacks(
32 $class->$method($cb),
33 );
34 }
35 }
36 }
37  
38 19 my @fallbacks = $class->_base_callbacks;
39  
40 # additional fallbacks based on explicitly provided callbacks
41  
42 19 foreach my $fallback (qw(__write print write syswrite)) {
43 70 ▶ if ( exists $user_cb{$fallback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $user_cb{$fallback}) 4 66 70
44 4 push @fallbacks, $class->_default_write_callbacks($fallback);
45 4 last;
46 }
47 }
48  
49 19 ▶ if ( exists $user_cb{getline} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $user_cb{"getline"}) 15 4 19
50 15 push @fallbacks, $class->_simple_getline_callbacks;
51 }
52  
53 19 ▶ if ( exists $user_cb{read} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $user_cb{"read"}) 15 4 19
54 15 push @fallbacks, $class->_simple_read_callbacks;
55 }
56  
57 # merge everything
58 19 my %cb = (
59 @fallbacks,
60 %user_cb,
61 );
62  
63 19 return \%cb;
64 }
65  
66 sub _base_callbacks {
67 19 ▶ my $class = shift;
Subroutine _base_callbacks: called CC=1 SCAR=4.1
68  
69 return (
70 0 ▶ fileno => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 ▶ stat => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 ▶ opened => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 blocking => sub {
74 0 ▶ my ( $self, @args ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
75  
76 0 ▶ Carp::croak("Can't set blocking mode on iterator") if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 0 0
77  
78 0 return 1;
79 },
80 19 );
81 }
82  
83 sub _make_read_callbacks {
84 15 ▶ my ( $class, $read ) = @_;
Subroutine _make_read_callbacks: called CC=1 SCAR=0.1
85  
86 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 return (
89 # these fallbacks must wrap the underlying reading mechanism
90 __read => sub {
91 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
92 0 ▶ if ( exists $self->{buf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'buf'}) { } 0 0 0
93 0 return delete $self->{buf};
94 } else {
95 0 my $ret = $self->$read;
96  
97 0 ▶ unless ( defined $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret) 0 0 0
98 0 $self->{eof}++;
99 }
100  
101 0 return $ret;
102 }
103 },
104 getline => sub {
105 65 ▶ my $self = shift;
Subroutine __ANON__: called CC=9 SCAR=22.0
106  
107 65 ▶ return undef if $self->{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 5 60 65
108  
109 60 ▶ if ( ref $/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $/) { } 5 55 60
elsif (defined $/) { } 50 5 55
110 5 $self->read(my $ret, ${$/});
111 5 return $ret;
112 } elsif ( defined $/ ) {
113 getline: {
114 50 ▶ if ( defined $self->{buf} and (my $off = index($self->{buf}, $/)) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'buf'} and (my $off = index($self->{'buf'}, $/)) > -1) { } 45 129 174
Condition100%3/3
defined $self->{"buf"} and (my $off = index($self->{"buf"}, $/)) > -1
!ll&&!rl&&r
1011945
MC/DC100%2/2
defined $self->{"buf"} and (my $off = index($self->{"buf"}, $/)) > -1
defined $self->{"buf"} (my $off = index($self->{"buf"}, $/)) > -1
Truth table100%3/3
A and B
A= defined $self->{"buf"}B= (my $off = index($self->{"buf"}, $/)) > -1
ABresult
0X0
100
111
115 45 return substr($self->{buf}, 0, $off + length($/), '');
116 } else {
117 129 ▶ if ( defined( my $chunk = $self->$read ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = $self->$read)) { } 124 5 129
118 124 $self->{buf} .= $chunk;
119 124 redo getline;
120 } else {
121 5 $self->{eof}++;
122  
123 5 ▶ if ( length( my $buf = delete $self->{buf} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $buf = delete $self->{'buf'})) { } 0 5 5
124 0 return $buf;
125 } else {
126 5 return undef;
127 }
128 }
129 }
130 }
131 } else {
132 5 my $ret = delete $self->{buf};
133  
134 5 ▶ while ( defined( my $chunk = $self->$read ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = $self->$read)) 26 5 31
135 26 $ret .= $chunk;
136 }
137  
138 5 $self->{eof}++;
139  
140 5 return $ret;
141 }
142 },
143 read => sub {
144 35 ▶ my ( $self, undef, $length, $offset ) = @_;
Subroutine __ANON__: called CC=9 SCAR=22.8
145  
146 35 ▶ return 0 if $self->{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 0 35 35
147  
148 35 ▶ if ( $offset and length($_[1]) < $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset and length $_[1] < $offset) 0 35 35
Condition66%2/3
$offset and length $_[1] < $offset
!ll&&!rl&&r
3050
MC/DC0%0/2
$offset and length $_[1] < $offset
$offset length $_[1] < $offset
Truth table66%2/3
A and B
A= $offsetB= length $_[1] < $offset
ABresult
0X0
100
111
149 0 $_[1] .= "\0" x ( $offset - length($_[1]) );
150 }
151  
152 35 ▶ while (length($self->{buf}) < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"buf"} < $length) 58 30 88
153 58 ▶ if ( defined(my $next = $self->$read) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $next = $self->$read)) { } 53 5 58
154 53 $self->{buf} .= $next;
155 } else {
156 # data ended but still under $length, return all that remains and
157 # empty the buffer
158 5 my $ret = length($self->{buf});
159  
160 5 ▶ if ( $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) { } 0 5 5
161 0 substr($_[1], $offset) = delete $self->{buf};
162 } else {
163 5 $_[1] = delete $self->{buf};
164 }
165  
166 5 $self->{eof}++;
167 5 return $ret;
168 }
169 }
170  
171 30 my $read;
172 30 ▶ if ( $length > length($self->{buf}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > length $self->{'buf'}) { } 0 30 30
173 0 $read = delete $self->{buf};
174 } else {
175 30 $read = substr($self->{buf}, 0, $length, '');
176 }
177  
178 30 ▶ if ( $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) { } 5 25 30
179 5 substr($_[1], $offset) = $read;
180 } else {
181 25 $_[1] = $read;
182 }
183  
184 30 return length($read);
185 },
186 eof => sub {
187 50 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
188 50 $self->{eof};
189 },
190 ungetc => sub {
191 5 ▶ my ( $self, $ord ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
192  
193 5 substr( $self->{buf}, 0, 0, chr($ord) );
194  
195 5 return;
196 },
197 15 );
198 }
199  
200 sub _default___read_callbacks {
201 6 ▶ my ( $class, $read ) = @_;
Subroutine _default___read_callbacks: called CC=1 SCAR=0.0
202  
203 6 $class->_make_read_callbacks($read);
204 }
205  
206 sub _default_read_callbacks {
207 3 ▶ my ( $class, $read ) = @_;
Subroutine _default_read_callbacks: called CC=1 SCAR=0.1
208  
209 $class->_make_read_callbacks(sub {
210 6 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.3
211  
212 6 ▶ if ( $self->$read(my $buf, ref $/ ? ${ $/ } : 4096) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->$read(my $buf, ref $/ ? ${$/;} : 4096)) { } 3 3 6
ref $/ ? : 0 6 6
213 3 return $buf;
214 } else {
215 3 return undef;
216 }
217 3 });
218 }
219  
220 sub _default_getline_callbacks {
221 6 ▶ my ( $class, $getline ) = @_;
Subroutine _default_getline_callbacks: called CC=1 SCAR=0.0
222  
223 $class->_make_read_callbacks(sub {
224 106 ▶ local $/ = ref $/ ? $/ : \4096;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $/ ? : 11 95 106
225 106 $_[0]->$getline;
226 6 });
227 }
228  
229 sub _simple_read_callbacks {
230 15 ▶ my $class = shift;
Subroutine _simple_read_callbacks: called CC=1 SCAR=0.4
231  
232 return (
233 # these are generic fallbacks defined in terms of the wrapping ones
234 sysread => sub {
235 0 ▶ shift->read(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 },
237 getc => sub {
238 10 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.6
239  
240 10 ▶ if ( $self->read(my $str, 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->read(my $str, 1)) { } 10 0 10
241 10 return $str;
242 } else {
243 0 return undef;
244 }
245 },
246 15 );
247 }
248  
249 sub _simple_getline_callbacks {
250 15 ▶ my $class = shift;
Subroutine _simple_getline_callbacks: called CC=1 SCAR=0.0
251  
252 return (
253 getlines => sub {
254 5 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
255  
256 5 my @accum;
257  
258 5 ▶ while ( defined(my $next = $self->getline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $next = $self->getline)) 25 5 30
259 25 push @accum, $next;
260 }
261  
262 5 return @accum;
263 }
264 15 );
265 }
266  
267 sub _default_write_callbacks {
268 4 ▶ my ( $class, $canonical ) = @_;
Subroutine _default_write_callbacks: called CC=1 SCAR=0.1
269  
270 return (
271 0 ▶ autoflush => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
272 ▶ sync => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
273 ▶ flush => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
274  
275 # these are defined in terms of a canonical print method, either write,
276 # syswrite or print
277 __write => sub {
278 14 ▶ my ( $self, $str ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
279 14 local $\;
280 14 local $,;
281 14 $self->$canonical($str);
282 },
283 print => sub {
284 10 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
285 10 ▶ my $ofs = defined $, ? $, : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $, ? : 8 2 10
286 10 ▶ my $ors = defined $\ ? $\ : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $\ ? : 8 2 10
287 10 $self->__write( join($ofs, @_) . $ors );
288 },
289  
290 (map { $_ => sub {
291 4 ▶ my ( $self, $str, $len, $offset ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
292 4 ▶ $len = length($str) unless defined $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 2 2 4
293 4 ▶ $offset ||= 0;
Condition100%2/2
$offset ||= 0
l!l
22
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
294 4 $self->__write(substr($str, $offset, $len));
295 8 } } qw(write syswrite)),
296  
297 # wrappers for print
298 printf => sub {
299 3 ▶ my ( $self, $f, @args ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
300 3 $self->print(sprintf $f, @args);
301 },
302 say => sub {
303 3 ▶ local $\ = "\n";
Subroutine __ANON__: called CC=1 SCAR=0.0
304 3 shift->print(@_);
305 },
306 printflush => sub {
307 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
308 0 my $autoflush = $self->autoflush;
309 0 my $ret = $self->print(@_);
310 0 $self->autoflush($autoflush);
311 0 return $ret;
312 }
313 4 );
314 }
315  
316 1 __PACKAGE__
317  
318 # ex: set sw=4 et:
319  
320 __END__
« Util.pm ↑ Summary Iterator.pm »