blib/lib/IO/Select.pm

filter: Statementstmt 82.9% 102 / 123 Branchbran 70.5% 55 / 78 Conditioncond 57.1% 24 / 42 MC/DCmcdc 28.0% 7 / 25 Subroutinesub 94.4% 17 / 18 Podpod 84.6% 11 / 13 totaltotal 72.2% 216 / 299 CC 58 SCAR 25.8
CC
58
Cov
74%
select
27.9
as_string
20.0
_update
15.7
CRAP 13.2
« blib/lib/IO/Socket.pm ↑ Summary blib/lib/IO/Socket/UNIX.pm »
1 # IO::Select.pm
2 #
3 # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6  
7 package IO::Select;
8  
9 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 require Exporter;
12  
13 9 our $VERSION = "1.55";
14  
15 9 our @ISA = qw(Exporter); # This is only so we can do version checking
16  
17 sub VEC_BITS () {0}
18 sub FD_COUNT () {1}
19 sub FIRST_FD () {2}
20  
21 sub new
22 50 ▶{
Subroutine new: called CC=3 SCAR=11.2
Pod documented
23 50 my $self = shift;
24 50 ▶ my $type = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
25  
26 50 my $vec = bless [undef,0], $type;
27  
28 50 ▶ $vec->add(@_)
Branch: 1 branch
BranchTrueFalseTotal
if @_ 48 2 50
29 if @_;
30  
31 50 $vec;
32 }
33  
34 sub add
35 55 ▶{
Subroutine add: called CC=1 SCAR=0.0
Pod documented
36 55 shift->_update('add', @_);
37 }
38  
39  
40 sub remove
41 61 ▶{
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
42 61 shift->_update('remove', @_);
43 }
44  
45  
46 sub exists
47 5 ▶{
Subroutine exists: called CC=2 SCAR=7.0
Pod documented
48 5 my $vec = shift;
49 5 my $fno = $vec->_fileno(shift);
50 5 ▶ return undef unless defined $fno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fno 0 5 5
51 5 $vec->[$fno + FIRST_FD];
52 }
53  
54  
55 sub _fileno
56 163 ▶{
Subroutine _fileno: called CC=8 SCAR=20.9
57 163 my($self, $f) = @_;
58 163 ▶ return unless defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 163 163
59 163 ▶ $f = $f->[0] if ref($f) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $f eq "ARRAY" 5 158 163
60 163 ▶ if($f =~ /^[0-9]+$/) { # plain file number
Branch: 2 branches
BranchTrueFalseTotal
if ($f =~ /^[0-9]+$/) { } 20 143 163
elsif (defined(my $fd = fileno $f)) { } 141 2 143
61 20 return $f;
62 }
63 elsif(defined(my $fd = fileno($f))) {
64 141 return $fd;
65 }
66 else {
67 # Neither a plain file number nor an opened filehandle; but maybe it was
68 # previously registered and has since been closed. ->remove still wants to
69 # know what fileno it had
70 2 foreach my $i ( FIRST_FD .. $#$self ) {
71 8 ▶ return $i - FIRST_FD if defined $self->[$i] && $self->[$i] == $f;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[$i] and $self->[$i] == $f 2 6 8
Condition100%3/3
defined $self->[$i] and $self->[$i] == $f
!ll&&!rl&&r
512
MC/DC100%2/2
defined $self->[$i] and $self->[$i] == $f
defined $self->[$i] $self->[$i] == $f
Truth table100%3/3
A and B
A= defined $self->[$i]B= $self->[$i] == $f
ABresult
0X0
100
111
72 }
73 0 return undef;
74 }
75 }
76  
77 sub _update
78 116 ▶{
Subroutine _update: called CC=12 SCAR=27.6
79 116 my $vec = shift;
80 116 my $add = shift eq 'add';
81  
82 116 my $bits = $vec->[VEC_BITS];
83 116 ▶ $bits = '' unless defined $bits;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bits 51 65 116
84  
85 116 my $count = 0;
86 116 my $f;
87 116 foreach $f (@_)
88 {
89 158 my $fn = $vec->_fileno($f);
90 158 ▶ if ($add) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add) { } 88 70 158
91 88 ▶ next unless defined $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn 0 88 88
92 88 my $i = $fn + FIRST_FD;
93 88 ▶ if (defined $vec->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vec->[$i]) 1 87 88
94 1 $vec->[$i] = $f; # if array rest might be different, so we update
95 1 next;
96 }
97 87 $vec->[FD_COUNT]++;
98 87 vec($bits, $fn, 1) = 1;
99 87 $vec->[$i] = $f;
100 } else { # remove
101 70 ▶ if ( ! defined $fn ) { # remove if fileno undef'd
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fn) { } 0 70 70
102 0 $fn = 0;
103 0 for my $fe (@{$vec}[FIRST_FD .. $#$vec]) {
104 0 ▶ if (defined($fe) && $fe == $f) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fe and $fe == $f) 0 0 0
Condition0%0/3
defined $fe and $fe == $f
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fe and $fe == $f
defined $fe $fe == $f
Truth table0%0/3
A and B
A= defined $feB= $fe == $f
ABresult
0X0
100
111
105 0 $vec->[FD_COUNT]--;
106 0 $fe = undef;
107 0 vec($bits, $fn, 1) = 0;
108 0 last;
109 }
110 0 ++$fn;
111 }
112 }
113 else {
114 70 my $i = $fn + FIRST_FD;
115 70 ▶ next unless defined $vec->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vec->[$i] 3 67 70
116 67 $vec->[FD_COUNT]--;
117 67 vec($bits, $fn, 1) = 0;
118 67 $vec->[$i] = undef;
119 }
120 }
121 154 $count++;
122 }
123 116 ▶ $vec->[VEC_BITS] = $vec->[FD_COUNT] ? $bits : undef;
Branch: 1 branch
BranchTrueFalseTotal
$vec->[1] ? : 85 31 116
124 116 $count;
125 }
126  
127 sub can_read
128 217 ▶{
Subroutine can_read: called CC=3 SCAR=11.0
Pod documented
129 217 my $vec = shift;
130 217 my $timeout = shift;
131 217 my $r = $vec->[VEC_BITS];
132  
133 217 ▶ defined($r) && (select($r,undef,undef,$timeout) > 0)
Branch: 1 branch
BranchTrueFalseTotal
defined $r && CORE::select($r, undef, undef, $timeout) > 0 ? : 188 29 217
Condition100%3/3
defined $r and CORE::select($r, undef, undef, $timeout) > 0
!ll&&!rl&&r
281188
MC/DC100%2/2
defined $r and CORE::select($r, undef, undef, $timeout) > 0
defined $r CORE::select($r, undef, undef, $timeout) > 0
Truth table100%3/3
A and B
A= defined $rB= CORE::select($r, undef, undef, $timeout) > 0
ABresult
0X0
100
111
134 ? handles($vec, $r)
135 : ();
136 }
137  
138 sub can_write
139 1 ▶{
Subroutine can_write: called CC=3 SCAR=12.0
Pod documented
140 1 my $vec = shift;
141 1 my $timeout = shift;
142 1 my $w = $vec->[VEC_BITS];
143  
144 1 ▶ defined($w) && (select(undef,$w,undef,$timeout) > 0)
Branch: 1 branch
BranchTrueFalseTotal
defined $w && CORE::select(undef, $w, undef, $timeout) > 0 ? : 1 0 1
Condition33%1/3
defined $w and CORE::select(undef, $w, undef, $timeout) > 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $w and CORE::select(undef, $w, undef, $timeout) > 0
defined $w CORE::select(undef, $w, undef, $timeout) > 0
Truth table33%1/3
A and B
A= defined $wB= CORE::select(undef, $w, undef, $timeout) > 0
ABresult
0X0
100
111
145 ? handles($vec, $w)
146 : ();
147 }
148  
149 sub has_exception
150 2 ▶{
Subroutine has_exception: called CC=3 SCAR=12.0
Pod documented
151 2 my $vec = shift;
152 2 my $timeout = shift;
153 2 my $e = $vec->[VEC_BITS];
154  
155 2 ▶ defined($e) && (select(undef,undef,$e,$timeout) > 0)
Branch: 1 branch
BranchTrueFalseTotal
defined $e && CORE::select(undef, undef, $e, $timeout) > 0 ? : 0 2 2
Condition33%1/3
defined $e and CORE::select(undef, undef, $e, $timeout) > 0
!ll&&!rl&&r
200
MC/DC0%0/2
defined $e and CORE::select(undef, undef, $e, $timeout) > 0
defined $e CORE::select(undef, undef, $e, $timeout) > 0
Truth table33%1/3
A and B
A= defined $eB= CORE::select(undef, undef, $e, $timeout) > 0
ABresult
0X0
100
111
156 ? handles($vec, $e)
157 : ();
158 }
159  
160 sub has_error
161 2 ▶{
Subroutine has_error: called CC=2 SCAR=6.9
Pod undocumented
162 2 ▶ warnings::warn("Call to deprecated method 'has_error', use 'has_exception'")
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 1 1 2
163 if warnings::enabled();
164 2 goto &has_exception;
165 }
166  
167 sub count
168 9 ▶{
Subroutine count: called CC=1 SCAR=0.0
Pod documented
169 9 my $vec = shift;
170 9 $vec->[FD_COUNT];
171 }
172  
173 sub bits
174 6 ▶{
Subroutine bits: called CC=1 SCAR=0.0
Pod documented
175 6 my $vec = shift;
176 6 $vec->[VEC_BITS];
177 }
178  
179 sub as_string # for debugging
180 0 ▶{
Subroutine as_string: not called CC=4 SCAR=30.0
Pod undocumented
181 0 my $vec = shift;
182 0 my $str = ref($vec) . ": ";
183 0 my $bits = $vec->bits;
184 0 my $count = $vec->count;
185 0 ▶ $str .= defined($bits) ? unpack("b*", $bits) : "undef";
Branch: 1 branch
BranchTrueFalseTotal
defined $bits ? : 0 0 0
186 0 $str .= " $count";
187 0 my @handles = @$vec;
188 0 splice(@handles, 0, FIRST_FD);
189 0 for (@handles) {
190 0 ▶ $str .= " " . (defined($_) ? "$_" : "-");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
191 }
192 0 $str;
193 }
194  
195 sub _max
196 2 ▶{
Subroutine _max: called CC=4 SCAR=15.8
197 2 my($a,$b,$c) = @_;
198 2 ▶ $a > $b
Branch: 3 branches
BranchTrueFalseTotal
$a > $b ? : 0 2 2
$a > $c ? : 0 0 0
$b > $c ? : 1 1 2
199 ? $a > $c
200 ? $a
201 : $c
202 : $b > $c
203 ? $b
204 : $c;
205 }
206  
207 sub select
208 2 ▶{
Subroutine select: called CC=20 SCAR=33.3
Pod documented
209 shift
210 2 ▶ if defined $_[0] && !ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not ref $_[0] 0 2 2
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
200
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
211  
212 2 my($r,$w,$e,$t) = @_;
213 2 my @result = ();
214  
215 2 ▶ my $rb = defined $r ? $r->[VEC_BITS] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $r ? : 0 2 2
216 2 ▶ my $wb = defined $w ? $w->[VEC_BITS] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $w ? : 2 0 2
217 2 ▶ my $eb = defined $e ? $e->[VEC_BITS] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $e ? : 1 1 2
218  
219 2 ▶ if(select($rb,$wb,$eb,$t) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::select($rb, $wb, $eb, $t) > 0) 2 0 2
220 {
221 2 my @r = ();
222 2 my @w = ();
223 2 my @e = ();
224 2 ▶ my $i = _max(defined $r ? scalar(@$r)-1 : 0,
Branch: 3 branches
BranchTrueFalseTotal
defined $r ? : 0 2 2
defined $w ? : 2 0 2
defined $e ? : 1 1 2
225 defined $w ? scalar(@$w)-1 : 0,
226 defined $e ? scalar(@$e)-1 : 0);
227  
228 2 ▶ for( ; $i >= FIRST_FD ; $i--)
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 2) 7 2 9
229 {
230 7 my $j = $i - FIRST_FD;
231 7 ▶ push(@r, $r->[$i])
Branch: 1 branch
BranchTrueFalseTotal
if defined $rb and defined $r->[$i] and vec $rb, $j, 1 0 7 7
Condition33%2/6
defined $rb and defined $r->[$i] and vec $rb, $j, 1
!ll&&!rl&&r
700
defined $rb and defined $r->[$i]
!ll&&!rl&&r
700
MC/DC0%0/3
defined $rb and defined $r->[$i] and vec $rb, $j, 1
defined $rb defined $r->[$i] vec $rb, $j, 1
Truth table25%1/4
A and B and C
A= defined $rbB= defined $r->[$i]C= vec $rb, $j, 1
ABCresult
0XX0
10X0
1100
1111
232 if defined $rb && defined $r->[$i] && vec($rb, $j, 1);
233 7 ▶ push(@w, $w->[$i])
Branch: 1 branch
BranchTrueFalseTotal
if defined $wb and defined $w->[$i] and vec $wb, $j, 1 2 5 7
Condition66%4/6
defined $wb and defined $w->[$i] and vec $wb, $j, 1
!ll&&!rl&&r
502
defined $wb and defined $w->[$i]
!ll&&!rl&&r
052
MC/DC33%1/3
defined $wb and defined $w->[$i] and vec $wb, $j, 1
defined $wb defined $w->[$i] vec $wb, $j, 1
Truth table50%2/4
A and B and C
A= defined $wbB= defined $w->[$i]C= vec $wb, $j, 1
ABCresult
0XX0
10X0
1100
1111
234 if defined $wb && defined $w->[$i] && vec($wb, $j, 1);
235 7 ▶ push(@e, $e->[$i])
Branch: 1 branch
BranchTrueFalseTotal
if defined $eb and defined $e->[$i] and vec $eb, $j, 1 0 7 7
Condition83%5/6
defined $eb and defined $e->[$i] and vec $eb, $j, 1
!ll&&!rl&&r
610
defined $eb and defined $e->[$i]
!ll&&!rl&&r
241
MC/DC0%0/3
defined $eb and defined $e->[$i] and vec $eb, $j, 1
defined $eb defined $e->[$i] vec $eb, $j, 1
Truth table75%3/4
A and B and C
A= defined $ebB= defined $e->[$i]C= vec $eb, $j, 1
ABCresult
0XX0
10X0
1100
1111
236 if defined $eb && defined $e->[$i] && vec($eb, $j, 1);
237 }
238  
239 2 @result = (\@r, \@w, \@e);
240 }
241 2 @result;
242 }
243  
244  
245 sub handles
246 193 ▶{
Subroutine handles: called CC=5 SCAR=16.1
Pod documented
247 193 my $vec = shift;
248 193 my $bits = shift;
249 193 my @h = ();
250 193 my $i;
251 193 my $max = scalar(@$vec) - 1;
252  
253 193 ▶ for ($i = FIRST_FD; $i <= $max; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max) 1133 193 1326
254 {
255 1133 ▶ next unless defined $vec->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vec->[$i] 764 369 1133
256 369 ▶ push(@h, $vec->[$i])
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bits or vec $bits, $i - 2, 1 225 144 369
Condition100%3/3
not defined $bits or vec $bits, $i - 2, 1
l!l&&r!l&&!r
12213144
MC/DC100%2/2
not defined $bits or vec $bits, $i - 2, 1
not defined $bits vec $bits, $i - 2, 1
Truth table100%3/3
not(A) or B
A= not defined $bitsB= vec $bits, $i - 2, 1
ABresult
1X1
011
000
257 if !defined($bits) || vec($bits, $i - FIRST_FD, 1);
258 }
259
260 193 @h;
261 }
262  
263 9 1;
264 __END__
« blib/lib/IO/Socket.pm ↑ Summary blib/lib/IO/Socket/UNIX.pm »