Lambda.pm

filter: Statementstmt 87.8% 705 / 803 Branchbran 60.6% 274 / 452 Conditioncond 50.3% 68 / 135 MC/DCmcdc 23.2% 17 / 73 Subroutinesub 84.9% 135 / 159 Podpod 82.0% 64 / 78 totaltotal 74.2% 1263 / 1700 CC 279 SCAR 20.8
CC
279
Cov
75%
__ANON__
72.0
_ev
56.0
__ANON__
22.4
CRAP 8.0
« Lambda/DNS.pm ↑ Summary Lambda/HTTP/UserAgent.pm »
1 package IO::Lambda;
2
3 30 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 $LOOP %EVENTS @LOOPS
12 $VERSION @ISA
13 @EXPORT_OK %EXPORT_TAGS @EXPORT_CONSTANTS @EXPORT_LAMBDA @EXPORT_STREAM
14 @EXPORT_DEV @EXPORT_FRAME @EXPORT_MISC @EXPORT_FUNC
15 $THIS @CONTEXT $METHOD $CALLBACK $AGAIN $SIGTHROW
16 $DEBUG_IO $DEBUG_LAMBDA $DEBUG_CALLER %DEBUG
17 30 );
18 30 $VERSION = '1.34';
19 30 @ISA = qw(Exporter);
20 30 @EXPORT_CONSTANTS = qw(
21 IO_READ IO_WRITE IO_EXCEPTION
22 WATCH_OBJ WATCH_DEADLINE WATCH_LAMBDA WATCH_CALLBACK
23 WATCH_IO_HANDLE WATCH_IO_FLAGS WATCH_CALLER WATCH_CANCEL
24 );
25 30 @EXPORT_STREAM = qw(
26 sysreader syswriter getline readbuf writebuf
27 );
28 30 @EXPORT_LAMBDA = qw(
29 this context lambda again state restartable catch autocatch
30 io readable writable rwx timeout tail tails tailo any_tail
31 );
32 30 @EXPORT_FUNC = qw(
33 seq par mapcar filter fold curry
34 );
35 30 @EXPORT_MISC = qw(
36 delete_frame set_frame get_frame swap_frame sigthrow
37 );
38 30 @EXPORT_DEV = qw(
39 _subname _o _t
40 );
41 30 @EXPORT_OK = (
42 @EXPORT_LAMBDA, @EXPORT_CONSTANTS, @EXPORT_STREAM,
43 @EXPORT_DEV, @EXPORT_MISC, @EXPORT_FUNC,
44 );
45 30 %EXPORT_TAGS = (
46 func => \@EXPORT_FUNC,
47 lambda => \@EXPORT_LAMBDA,
48 stream => \@EXPORT_STREAM,
49 constants => \@EXPORT_CONSTANTS,
50 dev => \@EXPORT_DEV,
51 all => [ @EXPORT_LAMBDA, @EXPORT_STREAM, @EXPORT_CONSTANTS, @EXPORT_FUNC ],
52 );
53
54 30 ▶if ( exists $ENV{IO_LAMBDA_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"IO_LAMBDA_DEBUG"}) 0 30 30
55 0 for my $p ( split ',', $ENV{IO_LAMBDA_DEBUG}) {
56 0 ▶ if ( $p =~ /^([^=]+)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /^([^=]+)=(.*)$/) { } 0 0 0
57 0 $DEBUG{lc $1}=$2;
58 } else {
59 0 $DEBUG{lc $p}++;
60 }
61 }
62 0 ▶ $DEBUG_IO = $DEBUG{io} || 0;
Condition0%0/2
$IO::Lambda::DEBUG{'io'} || 0
l!l
00
MC/DC0%0/1
$IO::Lambda::DEBUG{'io'} || 0
$IO::Lambda::DEBUG{'io'}
Truth table0%0/2
A
A= $IO::Lambda::DEBUG{'io'}
Aresult
11
00
63 0 ▶ $DEBUG_LAMBDA = $DEBUG{lambda} || 0;
Condition0%0/2
$IO::Lambda::DEBUG{'lambda'} || 0
l!l
00
MC/DC0%0/1
$IO::Lambda::DEBUG{'lambda'} || 0
$IO::Lambda::DEBUG{'lambda'}
Truth table0%0/2
A
A= $IO::Lambda::DEBUG{'lambda'}
Aresult
11
00
64 0 ▶ $DEBUG_CALLER = $DEBUG{caller} || 0;
Condition0%0/2
$IO::Lambda::DEBUG{'caller'} || 0
l!l
00
MC/DC0%0/1
$IO::Lambda::DEBUG{'caller'} || 0
$IO::Lambda::DEBUG{'caller'}
Truth table0%0/2
A
A= $IO::Lambda::DEBUG{'caller'}
Aresult
11
00
65 0 ▶ $IO::Lambda::Loop::DEFAULT = $DEBUG{loop} if $DEBUG{loop};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG{"loop"} 0 0 0
66 0 ▶ $SIG{__DIE__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
67 0 ▶ return if $^S;
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 0 0
68 0 Carp::confess(@_);
69 0 ▶ } if $DEBUG{die};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG{"die"} 0 0 0
70 }
71
72 30 ▶use constant IO_READ => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 30 ▶use constant IO_WRITE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 30 ▶use constant IO_EXCEPTION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
75
76 30 ▶use constant WATCH_OBJ => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 30 ▶use constant WATCH_CANCEL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
78
79 30 ▶use constant WATCH_DEADLINE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 30 ▶use constant WATCH_LAMBDA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 30 ▶use constant WATCH_CALLBACK => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
82
83 30 ▶use constant WATCH_CALLER => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 30 ▶use constant WATCH_IO_HANDLE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 30 ▶use constant WATCH_IO_FLAGS => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
86
87 sub new
88 456 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
89 456 ▶ IO::Lambda::Loop-> new unless $LOOP;
Branch: 1 branch
BranchTrueFalseTotal
unless $IO::Lambda::LOOP 27 429 456
90 456 return bless {
91 in => [], # events we wait for
92 last => [], # result of the last state
93 stopped => 0, # initial state
94 start => $_[1], # kick-start coderef
95 }, $_[0];
96 }
97
98 sub DESTROY
99 432 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
100 432 my $self = $_[0];
101 432 $self-> cancel_all_events;
102 }
103
104 30 my $_doffs = 0;
105 0 ▶sub _d_in { $_doffs++ }
Subroutine _d_in: not called CC=1 SCAR=6.9
106 0 ▶sub _d_out { $_doffs-- if $_doffs }
Subroutine _d_out: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_doffs 0 0 0
107 0 ▶sub _d { (' ' x $_doffs), _obj(shift), ': ', @_, "\n" }
Subroutine _d: not called CC=1 SCAR=6.9
108 0 ▶sub _o { $_[0] =~ /0x([\w]+)/; $1 }
Subroutine _o: not called CC=1 SCAR=6.9
109 0 ▶sub _obj { "lambda(". _o($_[0]) . ")." . ( $_[0]->{caller} || '()' ) }
Subroutine _obj: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]{'caller'} || '()'
l!l
00
MC/DC0%0/1
$_[0]{'caller'} || '()'
$_[0]{'caller'}
Truth table0%0/2
A
A= $_[0]{'caller'}
Aresult
11
00
110 0 ▶sub _t { defined($_[0]) ? ( "time(", (($_[0] < 1_000_000) ? $_[0] : $_[0]-time()), ")" ) : () }
Subroutine _t: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
$_[0] < 1000000 ? : 0 0 0
111 sub _ev
112 0 ▶{
Subroutine _ev: not called CC=7 SCAR=40.3
113 0 $_[0] =~ /0x([\w]+)/;
114 "event($1) ",
115 0 ▶ (($#{$_[0]} == WATCH_IO_FLAGS) ? (
Branch: 6 branches
BranchTrueFalseTotal
$#{$_[0];} == 5 ? : 0 0 0
$_[0][5] ? : 0 0 0
$_[0][5] & 4 ? : 0 0 0
$_[0][5] & 2 ? : 0 0 0
$_[0][5] & 1 ? : 0 0 0
ref $_[0][2] ? : 0 0 0
116 'fd=',
117 fileno($_[0]->[WATCH_IO_HANDLE]),
118 ' ',
119 ( $_[0]->[WATCH_IO_FLAGS] ? (
120 join('/',
121 (($_[0]->[WATCH_IO_FLAGS] & IO_READ) ? 'read' : ()),
122 (($_[0]->[WATCH_IO_FLAGS] & IO_WRITE) ? 'write' : ()),
123 (($_[0]->[WATCH_IO_FLAGS] & IO_EXCEPTION) ? 'exc' : ()),
124 )) :
125 'timeout'
126 ),
127 ' ', _t($_[0]->[WATCH_DEADLINE]),
128 ) : (
129 ref($_[0]-> [WATCH_LAMBDA]) ?
130 _obj($_[0]-> [WATCH_LAMBDA]) :
131 _t($_[0]->[WATCH_DEADLINE])
132 ))
133 }
134 sub _msg
135 0 ▶{
Subroutine _msg: not called CC=2 SCAR=17.9
136 0 my $self = shift;
137 _d(
138 $self,
139 "@_ >> (",
140 join(',',
141 map {
142 0 ▶ defined($_) ? $_ : 'undef'
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
143 }
144 0 @{$self->{last}}
145 ),
146 ')'
147 )
148 }
149
150 #
151 # Part I - Object interface to callback and
152 # messaging interface with event loop and lambdas
153 #
154 #########################################################
155
156 # register an IO event
157 sub watch_io
158 127 ▶{
Subroutine watch_io: called CC=6 SCAR=18.3
Pod documented
159 127 my ( $self, $flags, $handle, $deadline, $callback, $cancel) = @_;
160
161 127 ▶ confess "can't register events on a stopped lambda" if $self-> {stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 127 127
162 127 ▶ confess "bad io flags" if 0 == ($flags & (IO_READ|IO_WRITE|IO_EXCEPTION));
Branch: 1 branch
BranchTrueFalseTotal
if 0 == ($flags & 7) 0 127 127
163
164 127 ▶ $deadline += time if defined($deadline) and $deadline < 1_000_000_000;
Branch: 1 branch
BranchTrueFalseTotal
if defined $deadline and $deadline < 1000000000 20 107 127
Condition66%2/3
defined $deadline and $deadline < 1000000000
!ll&&!rl&&r
107020
MC/DC50%1/2
defined $deadline and $deadline < 1000000000
defined $deadline $deadline < 1000000000
Truth table66%2/3
A and B
A= defined $deadlineB= $deadline < 1000000000
ABresult
0X0
100
111
165
166 127 my $rec = [
167 $self,
168 $cancel,
169 $deadline,
170 $callback,
171 $handle,
172 $flags,
173 ];
174 127 weaken $rec->[0];
175 127 push @{$self-> {in}}, $rec;
176
177 127 ▶ warn _d( $self, "> ", _ev($rec)) if $DEBUG_IO;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_IO 0 127 127
178
179 127 $LOOP-> watch( $rec );
180
181 127 return $rec;
182 }
183
184 # register a timeout
185 sub watch_timer
186 54 ▶{
Subroutine watch_timer: called CC=5 SCAR=16.3
Pod documented
187 54 my ( $self, $deadline, $callback, $cancel) = @_;
188
189 54 ▶ confess "can't register events on a stopped lambda" if $self-> {stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 54 54
190 54 ▶ confess "$self: time is undefined" unless defined $deadline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $deadline 0 54 54
191
192 54 ▶ $deadline += time if $deadline < 1_000_000_000;
Branch: 1 branch
BranchTrueFalseTotal
if $deadline < 1000000000 51 3 54
193 54 my $rec = [
194 $self,
195 $cancel,
196 $deadline,
197 $callback,
198 ];
199 54 weaken $rec->[0];
200 54 push @{$self-> {in}}, $rec;
201
202 54 ▶ warn _d( $self, "> ", _ev($rec)) if $DEBUG_IO;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_IO 0 54 54
203
204 54 $LOOP-> after( $rec);
205
206 54 return $rec;
207 }
208
209 # register a callback when another lambda exits
210 sub watch_lambda
211 400 ▶{
Subroutine watch_lambda: called CC=9 SCAR=23.2
Pod documented
212 400 my ( $self, $lambda, $callback, $cancel) = @_;
213 400 @_ = (); # perl bug http://rt.perl.org/rt3//Public/Bug/Display.html?id=70974
214
215 400 ▶ confess "can't register events on a stopped lambda" if $self-> {stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 400 400
216 400 ▶ confess "bad lambda" unless $lambda and $lambda->isa('IO::Lambda');
Branch: 1 branch
BranchTrueFalseTotal
unless $lambda and $lambda->isa("IO::Lambda") 0 400 400
Condition33%1/3
$lambda and $lambda->isa("IO::Lambda")
!ll&&!rl&&r
00400
MC/DC0%0/2
$lambda and $lambda->isa("IO::Lambda")
$lambda $lambda->isa("IO::Lambda")
Truth table33%1/3
A and B
A= $lambdaB= $lambda->isa("IO::Lambda")
ABresult
0X0
100
111
217
218 400 ▶ confess "won't watch myself" if $self == $lambda;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $lambda 0 400 400
219 # XXX check cycling
220
221 400 ▶ $lambda-> reset if $lambda-> is_stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $lambda->is_stopped 0 400 400
222
223 400 my $rec = [
224 $self,
225 $cancel,
226 $lambda,
227 $callback,
228 ];
229 400 weaken $rec->[0];
230 400 ▶ $rec-> [WATCH_CALLER] = Carp::shortmess if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 400 400
231 400 push @{$self-> {in}}, $rec;
232 400 push @{$EVENTS{"$lambda"}}, $rec;
233
234 400 ▶ $lambda-> start if $lambda-> is_passive;
Branch: 1 branch
BranchTrueFalseTotal
if $lambda->is_passive 356 44 400
235
236 395 ▶ warn _d( $self, "> ", _ev($rec)) if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 395 395
237
238 395 return $rec;
239 }
240
241 # watch the watchers
242 sub override
243 33 ▶{
Subroutine override: called CC=12 SCAR=25.8
Pod documented
244 33 ▶ my ( $self, $method, $state, $cb) = ( 4 == @_) ? @_ : (@_[0,1],'*',$_[2]);
Branch: 1 branch
BranchTrueFalseTotal
4 == @_ ? : 14 19 33
245
246 33 ▶ if ( $cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 13 20 33
247 13 ▶ $self-> {override}->{$method} ||= [];
Condition100%2/2
$self->{"override"}{$method} ||= []
l!l
67
MC/DC100%1/1
$self->{"override"}{$method} ||= []
$self->{"override"}{$method}
Truth table100%2/2
A
A= $self->{"override"}{$method}
Aresult
11
00
248 13 push @{$self-> {override}->{$method}}, [ $state, $cb ];
249 } else {
250 20 my $p;
251 20 ▶ return unless $p = $self-> {override}->{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless $p = $self->{"override"}{$method} 1 19 20
252 19 ▶ for ( my $i = $#$p; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 21 0 21
253 21 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $state || defined $p->[$i][0] or defined $state and defined $p->[$i][0] and $p->[$i][0] eq $state) 19 2 21
Condition50%6/12
not defined $state || defined $p->[$i][0] or defined $state and defined $p->[$i][0] and $p->[$i][0] eq $state
l!l&&r!l&&!r
0192
defined $state || defined $p->[$i][0]
l!l&&r!l&&!r
2100
defined $state and defined $p->[$i][0] and $p->[$i][0] eq $state
!ll&&!rl&&r
0219
defined $state and defined $p->[$i][0]
!ll&&!rl&&r
0021
MC/DC20%1/5
not defined $state || defined $p->[$i][0] or defined $state and defined $p->[$i][0] and $p->[$i][0] eq $state
defined $state defined $p->[$i][0] defined $state defined $p->[$i][0] $p->[$i][0] eq $state
Truth table22%2/9
not(A || B) or C and D and E
A= defined $stateB= defined $p->[$i][0]C= defined $stateD= defined $p->[$i][0]E= $p->[$i][0] eq $state
ABCDEresult
00XXX1
1X1111
011111
1X0XX0
1X10X0
1X1100
010XX0
0110X0
011100
254 (
255 not defined ($state) and
256 not defined ($p->[$i]-> [0])
257 ) or (
258 defined($state) and
259 defined($p->[$i]-> [0]) and
260 $p->[$i]->[0] eq $state
261 )
262 ) {
263 19 my $ret = splice( @$p, $i, 1);
264 19 ▶ delete $self-> {override}->{$method} unless @$p;
Branch: 1 branch
BranchTrueFalseTotal
unless @$p 11 8 19
265 19 return $ret->[1];
266 }
267 }
268
269 0 return undef;
270 }
271 }
272
273 sub override_handler
274 40 ▶{
Subroutine override_handler: called CC=10 SCAR=23.1
Pod undocumented
275 40 my ( $self, $method, $sub, $cb) = @_;
276
277 40 my $o = $self-> {override}-> {$method}-> [-1];
278
279 # check state match
280 40 my ($a, $b) = ( $self-> {state}, $o-> [0]);
281 40 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $a || defined $b or defined $a and defined $b and $a eq $b or defined $b and $b eq '*') { } 32 8 40
Condition77%14/18
not defined $a || defined $b or defined $a and defined $b and $a eq $b or defined $b and $b eq "*"
l!l&&r!l&&!r
4288
not defined $a || defined $b or defined $a and defined $b and $a eq $b
l!l&&r!l&&!r
0436
defined $a || defined $b
l!l&&r!l&&!r
18220
defined $a and defined $b and $a eq $b
!ll&&!rl&&r
22144
defined $a and defined $b
!ll&&!rl&&r
22018
defined $b and $b eq "*"
!ll&&!rl&&r
0828
MC/DC28%2/7
not defined $a || defined $b or defined $a and defined $b and $a eq $b or defined $b and $b eq "*"
defined $a defined $b defined $a defined $b $a eq $b defined $b $b eq "*"
Truth table19%4/21
not(A || B) or C and D and E or F and G
A= defined $aB= defined $bC= defined $aD= defined $bE= $a eq $bF= defined $bG= $b eq "*"
ABCDEFGresult
00XXXXX1
1X111XX1
01111XX1
1X0XX111
1X10X111
1X110111
010XX111
0110X111
01110111
1X0XX0X0
1X0XX100
1X10X0X0
1X10X100
1X1100X0
1X110100
010XX0X0
010XX100
0110X0X0
0110X100
011100X0
01110100
282 ( not defined($a) and not defined ($b)) or
283 ( defined $a and defined $b and $a eq $b) or
284 ( defined $b and $b eq '*')
285 ) {
286 # state not matched
287 8 ▶ if ( 1 == @{$self-> {override}->{$method}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @{$self->{'override'}{$method};}) { } 4 4 8
288 4 local $self-> {override}->{$method} = undef;
289 4 return $sub-> ($cb);
290 } else {
291 4 pop @{$self-> {override}->{$method}};
292 4 my $ret = $sub-> ($cb);
293 4 push @{$self->{override}->{$method}}, $o;
294 4 return $ret;
295 }
296 } else {
297 # state matched
298 32 local $self-> {super} = [ $sub, $cb ];
299 32 ▶ if ( 1 == @{$self-> {override}->{$method}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @{$self->{'override'}{$method};}) { } 22 10 32
300 22 local $self-> {override}->{$method} = undef;
301 22 return $o-> [1]-> ( $self, $sub, $cb);
302 } else {
303 10 pop @{$self-> {override}->{$method}};
304 10 my $ret = $o-> [1]-> ( $self, $sub, $cb);
305 10 push @{$self->{override}->{$method}}, $o;
306 10 return $ret;
307 }
308 }
309 }
310
311 # Insert a new callback to be called before original callback.
312 # Needs to insert callbacks in {override} stack in reverse order,
313 # because direct order serves LIFO order for override() callbacks, --
314 # and that means FIFO for intercept() callbacks. But we also want LIFO.
315 sub intercept
316 24 ▶{
Subroutine intercept: called CC=4 SCAR=13.9
Pod documented
317 24 ▶ my ( $self, $method, $state, $cb) = ( 4 == @_) ? @_ : (@_[0,1],'*',$_[2]);
Branch: 1 branch
BranchTrueFalseTotal
4 == @_ ? : 4 20 24
318
319 24 ▶ return $self-> override( $method, $state, undef) unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 10 14 24
320
321 14 _subname("intercept($method:$state)" => $cb);
322
323 14 ▶ $self-> {override}->{$method} ||= [];
Condition100%2/2
$self->{"override"}{$method} ||= []
l!l
68
MC/DC100%1/1
$self->{"override"}{$method} ||= []
$self->{"override"}{$method}
Truth table100%2/2
A
A= $self->{"override"}{$method}
Aresult
11
00
324 14 ▶ unshift @{$self-> {override}->{$method}}, [ $state, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
325 # this is called when lambda calls $method with $state
326 17 my ( undef, $sub, $orig_cb) = @_;
327 # $sub is a condition, like readable(&) or tail(&)
328 16 ▶ $sub->( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
329 # that (&) is finally called when IO event is there
330 16 local $self-> {super} = [$orig_cb];
331 16 &$cb;
332 17 });
333 14 } ];
334 }
335
336 sub super
337 25 ▶{
Subroutine super: called CC=5 SCAR=16.2
Pod documented
338 25 ▶ confess "super() call outside overridden condition" unless $_[0]-> {super};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"super"} 0 25 25
339 25 my $data = $_[0]-> {super};
340 25 ▶ if ( defined $data-> [1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->[1]) { } 5 20 25
341 # override() super
342 5 return $data-> [0]-> ($data-> [1]);
343 } else {
344 # intercept() super
345 20 my $self = shift;
346 20 ▶ return defined($data->[0]) ?
Branch: 2 branches
BranchTrueFalseTotal
defined $data->[0] ? : 15 5 20
wantarray ? : 5 0 5
347 $data-> [0]-> (@_) :
348 ( wantarray ? @_ : $_[0] );
349 }
350 }
351
352
353 # handle incoming asynchronous events
354 sub io_handler
355 166 ▶{
Subroutine io_handler: called CC=11 SCAR=25.3
Pod undocumented
356 166 my ( $self, $rec) = @_;
357
358 166 ▶ warn _d( $self, '< ', _ev($rec)) if $DEBUG_IO;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_IO 0 166 166
359
360 166 my $in = $self-> {in};
361 166 my $nn = @$in;
362 166 @$in = grep { $rec != $_ } @$in;
363 166 ▶ die _d($self, 'stray ', _ev($rec))
Branch: 1 branch
BranchTrueFalseTotal
if $nn == @$in or $self != $rec->[0] 0 166 166
Condition33%1/3
$nn == @$in or $self != $rec->[0]
l!l&&r!l&&!r
00166
MC/DC0%0/2
$nn == @$in or $self != $rec->[0]
$nn == @$in $self != $rec->[0]
Truth table33%1/3
A or B
A= $nn == @$inB= $self != $rec->[0]
ABresult
1X1
011
000
364 if $nn == @$in or $self != $rec->[WATCH_OBJ];
365
366 166 ▶ _d_in if $DEBUG_IO;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_IO 0 166 166
367 166 local $self-> {cancel} = $rec-> [WATCH_CANCEL];
368 166 @{$self->{last}} = $rec-> [WATCH_CALLBACK]-> (
369 $self,
370 (($#$rec == WATCH_IO_FLAGS) ? $rec-> [WATCH_IO_FLAGS] : ()),
371 166 ▶ @{$self->{last}}
Branch: 2 branches
BranchTrueFalseTotal
if $rec->[3] 166 0 166
$rec->$#* == 5 ? : 121 45 166
372 ) if $rec-> [WATCH_CALLBACK];
373
374 166 ▶ _d_out if $DEBUG_IO;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_IO 0 166 166
375 166 ▶ warn $self-> _msg('io') if $DEBUG_IO;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_IO 0 166 166
376
377 166 ▶ unless ( @$in) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$in) 129 37 166
378 129 ▶ warn _d( $self, 'stopped') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 129 129
379 129 $self-> {stopped}++;
380 }
381 }
382
383 # handle incoming synchronous events
384 sub lambda_handler
385 369 ▶{
Subroutine lambda_handler: called CC=12 SCAR=25.7
Pod undocumented
386 369 my ( $self, $rec) = @_;
387
388 369 ▶ warn _d( $self, '< ', _ev($rec)) if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 369 369
389
390 369 my $in = $self-> {in};
391 369 my $nn = @$in;
392 369 @$in = grep { $rec != $_ } @$in;
393 369 ▶ die _d($self, 'stray ', _ev($rec))
Branch: 1 branch
BranchTrueFalseTotal
if $nn == @$in or $self != $rec->[0] 0 369 369
Condition33%1/3
$nn == @$in or $self != $rec->[0]
l!l&&r!l&&!r
00369
MC/DC0%0/2
$nn == @$in or $self != $rec->[0]
$nn == @$in $self != $rec->[0]
Truth table33%1/3
A or B
A= $nn == @$inB= $self != $rec->[0]
ABresult
1X1
011
000
394 if $nn == @$in or $self != $rec->[WATCH_OBJ];
395
396 369 my $lambda = $rec-> [WATCH_LAMBDA];
397 die _d($self,
398 'handler called but ', _obj($lambda),
399 369 ▶ ' is not finished yet') unless $lambda-> {stopped};
Branch: 1 branch
BranchTrueFalseTotal
unless $lambda->{"stopped"} 0 369 369
400
401 369 my $arr = $EVENTS{"$lambda"};
402 369 @$arr = grep { $_ != $rec } @$arr;
403 369 ▶ delete $EVENTS{"$lambda"} unless @$arr;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arr 366 3 369
404
405 369 ▶ _d_in if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 369 369
406
407 369 local $self-> {cancel} = $rec-> [WATCH_CANCEL];
408 369 @{$self->{last}} =
409 $rec-> [WATCH_CALLBACK] ?
410 $rec-> [WATCH_CALLBACK]-> (
411 $self,
412 351 @{$rec-> [WATCH_LAMBDA]-> {last}}
413 ) :
414 369 ▶ @{$rec-> [WATCH_LAMBDA]-> {last}};
Branch: 1 branch
BranchTrueFalseTotal
$rec->[3] ? : 351 18 369
415
416 369 ▶ _d_out if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 369 369
417 369 ▶ warn $self-> _msg('tail') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 369 369
418
419 369 ▶ unless ( @$in) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$in) 214 155 369
420 214 ▶ warn _d( $self, 'stopped') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 214 214
421 214 $self-> {stopped} = 1;
422 }
423 }
424
425 # Removes one event from queue
426 sub cancel_event
427 9 ▶{
Subroutine cancel_event: called CC=11 SCAR=24.5
Pod undocumented
428 9 my ( $self, $rec) = @_;
429
430 9 ▶ return unless @{$self-> {in}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"in"};} 1 8 9
431
432 0 ▶ @{$self->{last}} = $rec-> [WATCH_CANCEL]->($self, @{$self->{last}})
Branch: 1 branch
BranchTrueFalseTotal
if $rec->[1] 0 8 8
433 if $rec-> [WATCH_CANCEL];
434
435 8 ▶ $LOOP-> remove_event($rec) if $LOOP;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::LOOP 8 0 8
436 8 @{$self-> {in}} = grep { $_ != $rec } @{$self-> {in}};
437
438 8 ▶ if ($rec->[WATCH_LAMBDA] and ref($rec->[WATCH_LAMBDA])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->[2] and ref $rec->[2]) 5 3 8
Condition66%2/3
$rec->[2] and ref $rec->[2]
!ll&&!rl&&r
035
MC/DC50%1/2
$rec->[2] and ref $rec->[2]
$rec->[2] ref $rec->[2]
Truth table66%2/3
A and B
A= $rec->[2]B= ref $rec->[2]
ABresult
0X0
100
111
439 5 my $arr = $EVENTS{$rec->[WATCH_LAMBDA]};
440 5 ▶ if ( $arr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr) 3 2 5
441 3 @$arr = grep { $_ != $rec } @$arr;
442 3 ▶ delete $EVENTS{$rec->[WATCH_LAMBDA]} unless @$arr;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arr 3 0 3
443 }
444 }
445 8 @$rec = ();
446
447 8 ▶ return if @{$self->{in}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"in"};} 4 4 8
448 # that was the last event
449
450 4 ▶ warn _d( $self, 'stopped') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 4 4
451 4 $self-> {stopped} = 1;
452 4 $_-> remove( $self) for @LOOPS;
453 }
454
455 # Removes all events bound to the object, notifies the interested objects.
456 # The object becomes stopped immediately, so no new events will be allowed to register.
457 sub cancel_all_events
458 632 ▶{
Subroutine cancel_all_events: called CC=9 SCAR=22.0
Pod undocumented
459 632 my $self = shift;
460
461 632 $self-> {stopped} = 1;
462 632 delete $self->{frames};
463
464 632 ▶ return unless @{$self-> {in}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"in"};} 588 44 632
465
466 44 for ( grep { $_-> [WATCH_CANCEL] } reverse @{$self-> {in}}) {
467 14 my $wc = $_-> [WATCH_CANCEL];
468 14 $_-> [WATCH_CANCEL] = undef;
469 14 @{$self->{last}} = $wc-> ($self, $_, @{$self->{last}})
470 }
471
472 44 ▶ $LOOP-> remove( $self) if $LOOP;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::LOOP 44 0 44
473 44 $_-> remove($self) for @LOOPS;
474
475 44 for my $rec ( @{$self->{in}}) {
476 48 ▶ if ( ref($rec->[WATCH_LAMBDA])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rec->[2]) 28 20 48
477 28 my $arr = $EVENTS{$rec->[WATCH_LAMBDA]};
478 28 ▶ if ( $arr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr) 28 0 28
479 28 @$arr = grep { $_ != $rec } @$arr;
480 28 ▶ delete $EVENTS{$rec->[WATCH_LAMBDA]} unless @$arr;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arr 28 0 28
481 }
482 }
483 48 @$rec = ();
484 }
485
486 44 @{$self-> {in}} = ();
487 }
488
489 sub autorestart
490 75 ▶{
Subroutine autorestart: called CC=3 SCAR=12.7
Pod documented
491 $#_ ?
492 $_[0]-> {autorestart} = $_[1] :
493 ( exists($_[0]-> {autorestart}) ?
494 75 ▶ $_[0]-> {autorestart} : 1)
Branch: 2 branches
BranchTrueFalseTotal
$#_ ? : 0 75 75
exists $_[0]{'autorestart'} ? : 0 75 75
495 }
496 713 ▶sub is_stopped { $_[0]-> {stopped} }
Subroutine is_stopped: called CC=1 SCAR=0.0
Pod documented
497 5 ▶sub is_waiting { not($_[0]->{stopped}) and @{$_[0]->{in}} }
Subroutine is_waiting: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
not $_[0]{"stopped"} and @{$_[0]{"in"};} 3 2 5
498 565 ▶sub is_passive { not($_[0]->{stopped}) and not(@{$_[0]->{in}}) }
Subroutine is_passive: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0]{'stopped'} || @{$_[0]{'in'};}
l!l&&r!l&&!r
855502
MC/DC100%2/2
$_[0]{'stopped'} || @{$_[0]{'in'};}
$_[0]{'stopped'} @{$_[0]{'in'};}
Truth table100%3/3
A || B
A= $_[0]{'stopped'}B= @{$_[0]{'in'};}
ABresult
1X1
011
000
499 1101 ▶sub is_active { $_[0]->{stopped} or @{$_[0]->{in}} }
Subroutine is_active: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"stopped"} or @{$_[0]{"in"};} 2 1099 1101
500
501 # reset the state machine
502 sub reset
503 116 ▶{
Subroutine reset: called CC=2 SCAR=7.0
Pod documented
504 116 my $self = shift;
505
506 116 $self-> cancel_all_events;
507 116 @{$self-> {last}} = ();
508 116 delete $self-> {stopped};
509 116 ▶ warn _d( $self, 'reset') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 116 116
510 }
511
512 # start the state machine
513 sub start
514 508 ▶{
Subroutine start: called CC=7 SCAR=19.8
Pod documented
515 508 my $self = shift;
516
517 508 ▶ confess "can't start active lambda, call reset() first" if $self-> is_active;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_active 0 508 508
518
519 508 ▶ warn _d( $self, 'started') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 508 508
520 505 @{$self->{last}} = $self-> {start}-> ($self, @{$self->{last}})
521 508 ▶ if $self-> {start};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"start"} 505 3 508
522 495 ▶ warn $self-> _msg('initial') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 495 495
523
524 495 ▶ unless ( @{$self->{in}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"in"};}) 130 365 495
525 130 ▶ warn _d( $self, 'stopped') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 130 130
526 130 $self-> {stopped} = 1;
527 }
528 }
529
530 # peek into the current state
531 156 ▶sub peek { wantarray ? @{$_[0]->{last}} : $_[0]-> {last}-> [0] }
Subroutine peek: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 36 120 156
532
533 # pass initial parameters to lambda
534 sub call
535 445 ▶{
Subroutine call: called CC=2 SCAR=7.0
Pod documented
536 445 my $self = shift;
537
538 445 ▶ confess "can't call active lambda" if $self-> is_active;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_active 0 445 445
539
540 445 @{$self-> {last}} = @_;
541 445 $self;
542 }
543
544 # abandon all states and stop with constant message
545 sub terminate
546 83 ▶{
Subroutine terminate: called CC=2 SCAR=7.0
Pod documented
547 83 my ( $self, @error) = @_;
548
549 83 $self-> {last} = \@error;
550 83 $self-> cancel_all_events;
551 83 ▶ warn $self-> _msg('terminate') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 83 83
552 }
553
554 # propagate event destruction on all levels
555 sub destroy
556 0 ▶{
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
557 0 shift-> cancel_all_events( cascade => 1);
558 }
559
560 # synchronisation
561
562 # drives objects dependant on the other objects until all of them
563 # are stopped
564 30 my ($drive_reentrancy_refresh, $drive_reentrancy_depth) = (0,0);
565 sub drive
566 856557 ▶{
Subroutine drive: called CC=12 SCAR=26.0
Pod undocumented
567 856557 $drive_reentrancy_depth++;
568
569 856557 my $changed = 1;
570 856557 my $executed = 0;
571 856557 ▶ warn "IO::Lambda::drive --------\n" if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 856557 856557
572 856557 eval {
573 856557 ▶ while ( $changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 856847 856557 1713404
574 856847 $changed = 0;
575
576 # dispatch
577 856847 for my $rec ( map { @$_ } values %EVENTS) {
578 2263936 ▶ next unless $rec->[WATCH_LAMBDA]-> {stopped};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->[2]{"stopped"} 2263567 369 2263936
579 369 $changed = 1;
580 369 $executed++;
581 369 $rec->[WATCH_OBJ]-> lambda_handler( $rec);
582 369 ▶ $drive_reentrancy_refresh = 0, last if $drive_reentrancy_refresh;
Branch: 1 branch
BranchTrueFalseTotal
if $drive_reentrancy_refresh 0 369 369
583 }
584 856847 ▶ warn "IO::Lambda::drive .........\n" if $DEBUG_LAMBDA and $changed;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA and $changed 0 856847 856847
Condition33%1/3
$IO::Lambda::DEBUG_LAMBDA and $changed
!ll&&!rl&&r
85684700
MC/DC0%0/2
$IO::Lambda::DEBUG_LAMBDA and $changed
$IO::Lambda::DEBUG_LAMBDA $changed
Truth table33%1/3
A and B
A= $IO::Lambda::DEBUG_LAMBDAB= $changed
ABresult
0X0
100
111
585 }
586 };
587 856557 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 856557 856557
588 856557 ▶ warn "IO::Lambda::drive +++++++++\n" if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 856557 856557
589
590 856557 $drive_reentrancy_depth--;
591 856557 ▶ $drive_reentrancy_refresh++ if $executed && $drive_reentrancy_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $executed and $drive_reentrancy_depth 0 856557 856557
Condition66%2/3
$executed and $drive_reentrancy_depth
!ll&&!rl&&r
8563821750
MC/DC0%0/2
$executed and $drive_reentrancy_depth
$executed $drive_reentrancy_depth
Truth table66%2/3
A and B
A= $executedB= $drive_reentrancy_depth
ABresult
0X0
100
111
592
593 856557 return $executed;
594 }
595
596 # do one quant
597 sub yield
598 856557 ▶{
Subroutine yield: called CC=6 SCAR=17.9
Pod documented
599 856557 my $nonblocking = shift;
600 856557 my $more_events = 0;
601
602 # custom loops must not wait
603 856557 for ( @LOOPS) {
604 856371 ▶ next if $_-> empty;
Branch: 1 branch
BranchTrueFalseTotal
if $_->empty 112 856259 856371
605 856259 $_-> yield;
606 856259 $more_events = 1;
607 }
608
609 856557 ▶ if ( drive) {
Branch: 1 branch
BranchTrueFalseTotal
if (drive()) 175 856382 856557
610 # some callbacks we called, don't let them wait in sleep
611 175 return 1;
612 }
613
614 # main loop waits, if anything
615 856382 ▶ unless ( $LOOP-> empty) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IO::Lambda::LOOP->empty) 173 856209 856382
616 173 $LOOP-> yield( $nonblocking);
617 173 $more_events = 1;
618 }
619
620 856382 ▶ $more_events = 1 if keys %EVENTS;
Branch: 1 branch
BranchTrueFalseTotal
if keys %IO::Lambda::EVENTS 856329 53 856382
621 856382 return $more_events;
622 }
623
624
625 # wait for one lambda to stop
626 sub wait
627 158 ▶{
Subroutine wait: called CC=3 SCAR=11.0
Pod documented
628 158 my $self = shift;
629 158 ▶ if ( $self-> is_passive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_passive) 141 17 158
630 141 $self-> call(@_);
631 141 $self-> start;
632 }
633 150 my @frame = get_frame();
634 150 ▶ yield while not $self-> {stopped};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"stopped"} or do { yield() } 150 856557 856707
635 150 set_frame(@frame);
636 150 return $self-> peek;
637 }
638
639 # wait for all lambdas to stop
640 sub wait_for_all
641 0 ▶{
Subroutine wait_for_all: not called CC=4 SCAR=30.0
Pod documented
642 0 my @objects = @_;
643 0 ▶ return unless @objects;
Branch: 1 branch
BranchTrueFalseTotal
unless @objects 0 0 0
644 0 $_-> start for grep { $_-> is_passive } @objects;
645 0 my @ret;
646 0 my @frame = get_frame();
647 0 while ( 1) {
648 0 push @ret, map { $_-> peek } grep { $_-> {stopped} } @objects;
649 0 @objects = grep { not $_-> {stopped} } @objects;
650 0 ▶ last unless @objects;
Branch: 1 branch
BranchTrueFalseTotal
unless @objects 0 0 0
651 0 yield;
652 }
653 0 set_frame(@frame);
654 0 return @ret;
655 }
656
657 # wait for at least one lambda to stop, return those that stopped
658 sub wait_for_any
659 0 ▶{
Subroutine wait_for_any: not called CC=4 SCAR=30.0
Pod documented
660 0 my @objects = @_;
661 0 ▶ return unless @objects;
Branch: 1 branch
BranchTrueFalseTotal
unless @objects 0 0 0
662 0 $_-> start for grep { $_-> is_passive } @objects;
663 0 my @frame = get_frame();
664 0 while ( 1) {
665 0 my @n = grep { $_-> {stopped} } @objects;
666 0 ▶ set_frame(@frame), return @n if @n;
Branch: 1 branch
BranchTrueFalseTotal
if @n 0 0 0
667 0 yield;
668 }
669 0 set_frame(@frame);
670 0 return;
671 }
672
673 # run the event loop until no lambdas are left in the blocking state
674 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
675 0 my @frame = get_frame();
676 0 ▶ do {} while yield;
Branch: 1 branch
BranchTrueFalseTotal
yield() and do { do { () } } 0 0 0
677 0 set_frame(@frame);
678 }
679
680 #
681 # Part II - Procedural interface to the lambda-style programming
682 #
683 #################################################################
684
685 0 ▶sub _lambda_restart { die "lambda() is not restartable" }
Subroutine _lambda_restart: not called CC=1 SCAR=6.9
686 sub lambda(&)
687 408 ▶{
Subroutine lambda: called CC=3 SCAR=13.1
Pod documented
688 408 my $cb = _subname(lambda => $_[0]);
689 503 ▶ my $l = __PACKAGE__-> new( sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
690 # initial lambda code is usually executed by tail/tails inside another lambda,
691 # so protect the upper-level context
692 503 local *__ANON__ = "IO::Lambda::lambda::callback";
693 503 local $THIS = shift;
694 503 local @CONTEXT = ();
695 503 local $CALLBACK = $cb;
696 503 local $METHOD = \&_lambda_restart;
697 503 ▶ $cb ? $cb-> (@_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 503 0 503
698 408 });
699 408 ▶ if ( $DEBUG_CALLER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::DEBUG_CALLER) 0 408 408
700 0 ▶ if ( $DEBUG_CALLER > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::DEBUG_CALLER > 1) { } 0 0 0
701 0 $l-> {caller} = Carp::longmess;
702 0 chomp $l-> {caller};
703 0 $l-> {caller} =~ s/^ at //;
704 } else {
705 0 $l-> {caller} = join(':', (caller)[1,2]);
706 }
707 }
708 408 $l;
709 }
710
711 sub _subname
712 849 ▶{
Subroutine _subname: called CC=5 SCAR=23.0
713 849 ▶ subname(
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER and $_[1] and not $IO::Lambda::AGAIN 0 849 849
Condition25%2/8
$IO::Lambda::DEBUG_CALLER and $_[1] and not $IO::Lambda::AGAIN
!ll&&!rl&&r
84900
$IO::Lambda::DEBUG_CALLER and $_[1]
!ll&&!rl&&r
84900
$_[2] || 0
l!l
00
MC/DC0%0/3
$IO::Lambda::DEBUG_CALLER and $_[1] and not $IO::Lambda::AGAIN
$IO::Lambda::DEBUG_CALLER $_[1] not $IO::Lambda::AGAIN
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table25%1/4
A and B and not(C)
A= $IO::Lambda::DEBUG_CALLERB= $_[1]C= not $IO::Lambda::AGAIN
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
714 caller(1 + ($_[2] || 0)) . '::_'. $_[0],
715 $_[1]
716 ) if $DEBUG_CALLER and $_[1] and not $AGAIN;
717 849 return $_[1];
718 }
719
720 30 *io = \&lambda;
721
722 # re-enter the latest (or other) frame
723 sub again
724 105 ▶{
Subroutine again: called CC=5 SCAR=16.4
Pod documented
725 105 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 104 105
726 1 my $name = shift;
727 1 ▶ Carp::carp("no such frame:$name") unless exists $THIS->{frames}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $IO::Lambda::THIS->{"frames"}{$name} 0 1 1
728 1 ($METHOD, $CALLBACK, @CONTEXT) = @{ $THIS->{frames}->{$name} };
729 1 ▶ @CONTEXT = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
730 }
731 105 local $AGAIN = 1;
732 105 ▶ defined($METHOD) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $IO::Lambda::METHOD ? : 105 0 105
733 $METHOD-> ($CALLBACK) :
734 confess "again() outside of a restartable call"
735 }
736
737 # define context
738 320 ▶sub this { @_ ? ($THIS, @CONTEXT) = @_ : $THIS }
Subroutine this: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 66 254 320
739 820 ▶sub context { @_ ? (@CONTEXT) = @_ : @CONTEXT }
Subroutine context: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 430 390 820
740 183 ▶sub set_frame { ( $THIS, $METHOD, $CALLBACK, @CONTEXT) = @_ }
Subroutine set_frame: called CC=1 SCAR=0.0
Pod documented
741 166 ▶sub get_frame { ( $THIS, $METHOD, $CALLBACK, @CONTEXT) }
Subroutine get_frame: called CC=1 SCAR=0.0
Pod documented
742 0 ▶sub swap_frame { my @f = get_frame; set_frame(@_); @f }
Subroutine swap_frame: not called CC=1 SCAR=6.9
Pod documented
743 1 ▶sub clear { set_frame(); undef $AGAIN; }
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
744 0 ▶sub delete_frame { delete $THIS->{frames}->{$_[0]} }
Subroutine delete_frame: not called CC=1 SCAR=6.9
Pod documented
745
746 30 ▶END { ( $THIS, $METHOD, $CALLBACK, @CONTEXT) = (); }
Subroutine END: called CC=1 SCAR=0.0
747
748 sub state($)
749 17 ▶{
Subroutine state: called CC=4 SCAR=13.9
Pod documented
750 17 ▶ my $this = ($_[0] && ref($_[0])) ? shift(@_) : this;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] ? : 5 12 17
Condition66%2/3
$_[0] and ref $_[0]
!ll&&!rl&&r
0125
MC/DC50%1/2
$_[0] and ref $_[0]
$_[0] ref $_[0]
Truth table66%2/3
A and B
A= $_[0]B= ref $_[0]
ABresult
0X0
100
111
751 17 ▶ @_ ? $this-> {state} = $_[0] : return $this-> {state};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 12 5 17
752 }
753
754 sub restartable
755 1 ▶{
Subroutine restartable: called CC=2 SCAR=7.1
Pod documented
756 1 ▶ my $name = @_ ? $_[0] : join(':', caller);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
757 1 $THIS->{frames}->{$name} = [ $METHOD, $CALLBACK, @CONTEXT ];
758 1 return $name;
759 }
760
761 # exceptions and backtracing
762 sub catch(&$)
763 23 ▶{
Subroutine catch: called CC=5 SCAR=16.3
Pod documented
764 23 my ( $cb, $event) = @_;
765 23 my $who = (caller(1))[3];
766 23 my @ctx = @CONTEXT;
767 23 ▶ confess "catch callback already defined" if $event-> [WATCH_CANCEL];
Branch: 1 branch
BranchTrueFalseTotal
if $event->[1] 0 23 23
768 15 ▶ $event->[WATCH_CANCEL] = $cb ? sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
769 15 ▶ local *__ANON__ = "$who\:\:catch" if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 15 15
770 15 $THIS = shift;
771 15 local $THIS-> {cancelled_event} = shift;
772 15 local $THIS-> {cancelling} = 1;
773 15 @CONTEXT = @ctx;
774 15 $METHOD = undef;
775 15 $CALLBACK = undef;
776 15 $cb-> (@_);
777 23 ▶ } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 23 0 23
778
779 # if throw() happened before we even get here
780 23 ▶ $event->[WATCH_CALLBACK] = $event->[WATCH_CANCEL]
Branch: 1 branch
BranchTrueFalseTotal
if $event->[3] and $event->[3] == \&IO::Lambda::_throw 5 18 23
Condition66%2/3
$event->[3] and $event->[3] == \&IO::Lambda::_throw
!ll&&!rl&&r
0185
MC/DC50%1/2
$event->[3] and $event->[3] == \&IO::Lambda::_throw
$event->[3] $event->[3] == \&IO::Lambda::_throw
Truth table66%2/3
A and B
A= $event->[3]B= $event->[3] == \&IO::Lambda::_throw
ABresult
0X0
100
111
781 if $event->[WATCH_CALLBACK] && $event->[WATCH_CALLBACK] == \&_throw;
782
783 23 return $event;
784 }
785
786 sub call_again
787 3 ▶{
Subroutine call_again: called CC=3 SCAR=11.4
Pod documented
788 3 my $self = shift;
789 3 ▶ confess "called outside catch()" unless $self-> {cancelled_event};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cancelled_event"} 0 3 3
790 3 my $cb = $self-> {cancelled_event}->[WATCH_CALLBACK];
791 3 ▶ $cb->($self, @_) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 3 0 3
792 }
793
794 sub autocatch($)
795 7 ▶{
Subroutine autocatch: called CC=1 SCAR=0.0
Pod documented
796 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
797 2 this-> call_again;
798 2 this-> throw(@_);
799 7 } $_[0]
800 }
801
802 3 ▶sub is_cancelling { $_[0]-> {cancelling} }
Subroutine is_cancelling: called CC=1 SCAR=0.0
Pod documented
803
804 sub _throw
805 15 ▶{
Subroutine _throw: called CC=2 SCAR=7.1
806 15 my $self = shift;
807 15 ▶ warn _d( $self, 'throw') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 15 15
808 15 $self-> throw(@_);
809 }
810
811 sub throw
812 40 ▶{
Subroutine throw: called CC=5 SCAR=16.1
Pod documented
813 40 my ( $self, @error) = @_;
814 40 my @c = $self-> callers;
815 40 ▶ $_-> [WATCH_CALLBACK] = $_->[WATCH_CANCEL] || \&_throw for @c;
Condition100%2/2
$_->[1] || \&IO::Lambda::_throw
l!l
523
MC/DC100%1/1
$_->[1] || \&IO::Lambda::_throw
$_->[1]
Truth table100%2/2
A
A= $_->[1]
Aresult
11
00
816 40 $self-> terminate(@error);
817 40 ▶ $SIGTHROW->($self, @error) if $SIGTHROW and not @c;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::SIGTHROW and not @c 2 38 40
Condition66%2/3
$IO::Lambda::SIGTHROW and not @c
!ll&&!rl&&r
3802
MC/DC50%1/2
$IO::Lambda::SIGTHROW and not @c
$IO::Lambda::SIGTHROW not @c
Truth table66%2/3
A and not(B)
A= $IO::Lambda::SIGTHROWB= not @c
ABresult
0X0
100
111
818 40 return @error;
819 }
820
821 sub sigthrow
822 4 ▶{
Subroutine sigthrow: called CC=5 SCAR=16.3
Pod documented
823 4 ▶ shift if defined($_[0]) and (not(ref $_[0]) or ref($_[0]) ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and !ref($_[0]) || ref $_[0] ne "CODE" 2 2 4
Condition83%5/6
defined $_[0] and !ref($_[0]) || ref $_[0] ne "CODE"
!ll&&!rl&&r
112
not ref $_[0] or ref $_[0] ne "CODE"
l!l&&r!l&&!r
201
MC/DC66%2/3
defined $_[0] and !ref($_[0]) || ref $_[0] ne "CODE"
defined $_[0] not ref $_[0] ref $_[0] ne "CODE"
Truth table75%3/4
A and not(B) or C
A= defined $_[0]B= not ref $_[0]C= ref $_[0] ne "CODE"
ABCresult
0XX0
1000
11X1
1011
824 4 ▶ $SIGTHROW = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 0 4
825 4 return $SIGTHROW;
826 }
827
828 0 ▶sub callees { @{ $EVENTS{ "$_[0]" } || [] } }
Subroutine callees: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$IO::Lambda::EVENTS{"$_[0]"} || []
l!l
00
MC/DC0%0/1
$IO::Lambda::EVENTS{"$_[0]"} || []
$IO::Lambda::EVENTS{"$_[0]"}
Truth table0%0/2
A
A= $IO::Lambda::EVENTS{"$_[0]"}
Aresult
11
00
829 60 ▶sub callers { grep { $_[0] == $_-> [WATCH_LAMBDA] } map { @$_ } values %EVENTS }
Subroutine callers: called CC=1 SCAR=0.0
Pod documented
830
831 sub backtrace
832 6 ▶{
Subroutine backtrace: called CC=1 SCAR=0.0
Pod documented
833 6 require IO::Lambda::Backtrace;
834 6 IO::Lambda::Backtrace-> new($_[0], Carp::shortmess);
835 }
836
837 #
838 # Conditions:
839 #
840
841 # common wrapper for declaration of handle-watching user conditions
842 sub add_watch
843 42 ▶{
Subroutine add_watch: called CC=3 SCAR=11.1
Pod undocumented
844 42 my ($self, $cb, $method, $flags, $handle, $deadline, @ctx) = @_;
845 42 my $who;
846 42 ▶ $who = (caller(1))[3] if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 42 42
847 $self-> watch_io(
848 $flags, $handle, $deadline,
849 41 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
850 41 ▶ local *__ANON__ = "$who\:\:callback" if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 41 41
851 41 $THIS = shift;
852 41 @CONTEXT = @ctx;
853 41 $METHOD = $method;
854 41 $CALLBACK = $cb;
855 41 ▶ $cb ? $cb-> (@_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 41 0 41
856 },
857 42 ▶ ($AGAIN ? delete($self-> {cancel}) : undef),
Branch: 1 branch
BranchTrueFalseTotal
$IO::Lambda::AGAIN ? : 14 28 42
858 )
859 }
860
861 # rwx($flags,$handle,$deadline)
862 sub rwx(&)
863 0 ▶{
Subroutine rwx: not called CC=2 SCAR=17.9
Pod documented
864 return $THIS-> override_handler('rwx', \&rwx, shift)
865 0 ▶ if $THIS-> {override}->{rwx};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::THIS->{"override"}{"rwx"} 0 0 0
866
867 0 $THIS-> add_watch(
868 _subname(rwx => shift), \&rwx,
869 @CONTEXT[0,1,2,0,1,2]
870 )
871 }
872
873 # readable($handle,$deadline)
874 sub readable(&)
875 35 ▶{
Subroutine readable: called CC=2 SCAR=7.2
Pod documented
876 return $THIS-> override_handler('readable', \&readable, shift)
877 35 ▶ if $THIS-> {override}->{readable};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::THIS->{"override"}{"readable"} 0 35 35
878
879 35 $THIS-> add_watch(
880 _subname(readable => shift), \&readable, IO_READ,
881 @CONTEXT[0,1,0,1]
882 )
883 }
884
885 # writable($handle,$deadline)
886 sub writable(&)
887 7 ▶{
Subroutine writable: called CC=2 SCAR=7.2
Pod documented
888 return $THIS-> override_handler('writable', \&writable, shift)
889 7 ▶ if $THIS-> {override}->{writable};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::THIS->{"override"}{"writable"} 0 7 7
890
891 7 $THIS-> add_watch(
892 _subname(writable => shift), \&writable, IO_WRITE,
893 @CONTEXT[0,1,0,1]
894 )
895 }
896
897 # common wrapper for declaration of time-watching user conditions
898 sub add_timer
899 41 ▶{
Subroutine add_timer: called CC=3 SCAR=11.0
Pod undocumented
900 41 my ($self, $cb, $method, $deadline, @ctx) = @_;
901 41 my $who;
902 41 ▶ $who = (caller(1))[3] if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 41 41
903 $self-> watch_timer(
904 $deadline,
905 38 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
906 38 ▶ local *__ANON__ = "$who\:\:callback" if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 38 38
907 38 $THIS = shift;
908 38 @CONTEXT = @ctx;
909 38 $METHOD = $method;
910 38 $CALLBACK = $cb;
911 38 ▶ $cb ? $cb-> (@_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 37 1 38
912 },
913 41 ▶ ($AGAIN ? delete($self-> {cancel}) : undef),
Branch: 1 branch
BranchTrueFalseTotal
$IO::Lambda::AGAIN ? : 17 24 41
914 )
915 }
916
917 # timeout($deadline)
918 sub timeout(&)
919 41 ▶{
Subroutine timeout: called CC=2 SCAR=7.2
Pod documented
920 return $THIS-> override_handler('timeout', \&timeout, shift)
921 41 ▶ if $THIS-> {override}->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::THIS->{"override"}{"timeout"} 0 41 41
922 41 $THIS-> add_timer( _subname(timeout => shift), \&timeout, @CONTEXT[0,0])
923 }
924
925 # common wrapper for declaration of single lambda-watching user conditions
926 sub add_tail
927 306 ▶{
Subroutine add_tail: called CC=4 SCAR=13.9
Pod undocumented
928 306 my ($self, $cb, $method, $lambda, @ctx) = @_;
929 306 my $who;
930 306 ▶ $who = (caller(1))[3] if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 306 306
931 $self-> watch_lambda(
932 $lambda,
933 255 ▶ ($cb ? sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
934 255 ▶ local *__ANON__ = "$who\:\:callback" if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 255 255
935 255 $THIS = shift;
936 255 @CONTEXT = @ctx;
937 255 $METHOD = $method;
938 255 $CALLBACK = $cb;
939 255 $cb-> (@_);
940 } : undef),
941 306 ▶ ($AGAIN ? delete($self-> {cancel}) : undef),
Branch: 2 branches
BranchTrueFalseTotal
$cb ? : 288 18 306
$IO::Lambda::AGAIN ? : 85 221 306
942 );
943 }
944
945 # convert constant @param into a lambda
946 sub add_constant
947 0 ▶{
Subroutine add_constant: not called CC=1 SCAR=6.9
Pod undocumented
948 0 my ( $self, $cb, $method, @param) = @_;
949 $self-> add_tail (
950 _subname(constant => $cb), $method,
951 0 ▶ lambda { @param },
Subroutine __ANON__: not called CC=1 SCAR=6.9
952 @CONTEXT
953 0 );
954 }
955
956 # handle default condition logic given a lambda
957 sub condition
958 38 ▶{
Subroutine condition: called CC=6 SCAR=19.9
Pod documented
959 38 my ( $self, $cb, $method, $name) = @_;
960
961 return $THIS-> override_handler($name, $method, $cb)
962 38 ▶ if defined($name) and $THIS-> {override}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and $IO::Lambda::THIS->{"override"}{$name} 0 38 38
Condition66%2/3
defined $name and $IO::Lambda::THIS->{"override"}{$name}
!ll&&!rl&&r
2360
MC/DC0%0/2
defined $name and $IO::Lambda::THIS->{"override"}{$name}
defined $name $IO::Lambda::THIS->{"override"}{$name}
Truth table66%2/3
A and B
A= defined $nameB= $IO::Lambda::THIS->{"override"}{$name}
ABresult
0X0
100
111
963
964 38 my @ctx = @CONTEXT;
965
966 38 my $who;
967 38 ▶ if ( $DEBUG_CALLER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::DEBUG_CALLER) 0 38 38
968 0 ▶ $who = defined($name) ? $name : (caller(1))[3];
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
969 0 _subname($who, $cb, 2);
970 }
971
972 $THIS-> watch_lambda(
973 $self,
974 38 ▶ $cb ? sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
975 38 ▶ local *__ANON__ = "$who\:\:callback" if $DEBUG_CALLER;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_CALLER 0 38 38
976 38 $THIS = shift;
977 38 @CONTEXT = @ctx;
978 38 $METHOD = $method;
979 38 $CALLBACK = $cb;
980 38 $cb-> (@_);
981 } : undef
982 38 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 38 0 38
983 }
984
985 # dummy sub for empty calls for tails() family
986 sub _empty
987 2 ▶{
Subroutine _empty: called CC=2 SCAR=7.0
988 2 my ($name, $method, $cb) = @_;
989 2 my @ctx = context;
990 2 ▶ $THIS-> watch_lambda( IO::Lambda-> new, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
991 2 local *__ANON__ = "IO::Lambda::".$name."::callback";
992 2 @CONTEXT = @ctx;
993 2 $METHOD = $method;
994 2 $CALLBACK = $cb;
995 2 $cb-> ();
996 2 ▶ }) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 2 0 2
997 }
998
999 # tail( $lambda, @param) -- initialize $lambda with @param, and wait for it
1000 sub tail(&)
1001 347 ▶{
Subroutine tail: called CC=7 SCAR=19.5
Pod documented
1002 return $THIS-> override_handler('tail', \&tail, shift)
1003 347 ▶ if $THIS-> {override}->{tail};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::THIS->{"override"}{"tail"} 40 307 347
1004
1005 307 my ( $lambda, @param) = context;
1006 307 ▶ return _empty(tail => \&tail, shift) unless $lambda;
Branch: 1 branch
BranchTrueFalseTotal
unless $lambda 1 306 307
1007
1008 306 ▶ $lambda-> reset
Branch: 1 branch
BranchTrueFalseTotal
if $lambda->is_stopped and $lambda->autorestart 75 231 306
Condition66%2/3
$lambda->is_stopped and $lambda->autorestart
!ll&&!rl&&r
231075
MC/DC50%1/2
$lambda->is_stopped and $lambda->autorestart
$lambda->is_stopped $lambda->autorestart
Truth table66%2/3
A and B
A= $lambda->is_stoppedB= $lambda->autorestart
ABresult
0X0
100
111
1009 if $lambda-> is_stopped and $lambda-> autorestart;
1010 306 ▶ if ( @param) {
Branch: 1 branch
BranchTrueFalseTotal
if (@param) { } 162 144 306
1011 162 $lambda-> call( @param);
1012 } else {
1013 144 ▶ $lambda-> call unless $lambda-> is_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $lambda->is_active 138 6 144
1014 }
1015 306 $THIS-> add_tail( _subname(tail => shift), \&tail, $lambda, $lambda, @param);
1016 }
1017
1018
1019 # tails(@lambdas) -- wait for all lambdas to finish
1020 sub tails(&)
1021 16 ▶{
Subroutine tails: called CC=4 SCAR=13.9
Pod documented
1022 return $THIS-> override_handler('tails', \&tails, shift)
1023 16 ▶ if $THIS-> {override}->{tails};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::THIS->{"override"}{"tails"} 0 16 16
1024
1025 16 my $cb = _subname tails => $_[0];
1026 16 my @lambdas = context;
1027 16 my $n = $#lambdas;
1028 16 ▶ return _empty(tails => \&tails, $cb) unless @lambdas;
Branch: 1 branch
BranchTrueFalseTotal
unless @lambdas 1 15 16
1029
1030 15 my @ret;
1031 my $watcher;
1032 25 ▶ $watcher = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1033 25 $THIS = shift;
1034 25 push @ret, @_;
1035 25 ▶ return if $n--;
Branch: 1 branch
BranchTrueFalseTotal
if $n-- 17 8 25
1036
1037 8 local *__ANON__ = "IO::Lambda::tails::callback";
1038 8 @CONTEXT = @lambdas;
1039 8 $METHOD = \&tails;
1040 8 $CALLBACK = $cb;
1041 8 ▶ $cb ? $cb-> (@ret) : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 7 1 8
1042 15 };
1043 15 my $this = $THIS;
1044 15 $this-> watch_lambda( $_, $watcher) for @lambdas;
1045 }
1046
1047 # tailo(@lambdas) -- wait for all lambdas to finish, return ordered results
1048 sub tailo(&)
1049 1 ▶{
Subroutine tailo: called CC=4 SCAR=13.9
Pod documented
1050 return $THIS-> override_handler('tailo', \&tailo, shift)
1051 1 ▶ if $THIS-> {override}->{tailo};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::THIS->{"override"}{"tailo"} 0 1 1
1052
1053 1 my $cb = _subname tailo => $_[0];
1054 1 my @lambdas = context;
1055 1 my $n = $#lambdas;
1056 1 ▶ return _empty(tailo => \&tailo, $cb) unless @lambdas;
Branch: 1 branch
BranchTrueFalseTotal
unless @lambdas 0 1 1
1057
1058 1 my @ret;
1059 my $watcher;
1060 3 ▶ $watcher = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1061 3 my $curr = shift;
1062 3 $THIS = shift;
1063 3 $ret[ $curr ] = \@_;
1064 3 ▶ return if $n--;
Branch: 1 branch
BranchTrueFalseTotal
if $n-- 2 1 3
1065
1066 1 local *__ANON__ = "IO::Lambda::tailo::callback";
1067 1 @CONTEXT = @lambdas;
1068 1 $METHOD = \&tailo;
1069 1 $CALLBACK = $cb;
1070 1 @ret = map { @$_ } @ret;
1071 1 ▶ $cb ? $cb-> (@ret) : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 1 0 1
1072 1 };
1073 1 my $this = $THIS;
1074 1 ▶ for ( my $i = 0; $i < @lambdas; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lambdas) 3 1 4
1075 3 my $d = $i;
1076 $this-> watch_lambda(
1077 $lambdas[$i],
1078 3 ▶ sub { $watcher->($d, @_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1079 3 );
1080 };
1081 }
1082
1083 # any_tail($deadline,@lambdas) -- wait for any lambda to finish within time
1084 sub any_tail(&)
1085 6 ▶{
Subroutine any_tail: called CC=4 SCAR=14.0
Pod documented
1086 return $THIS-> override_handler('any_tail', \&any_tail, shift)
1087 6 ▶ if $THIS-> {override}->{any_tail};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::THIS->{"override"}{"any_tail"} 0 6 6
1088
1089 6 my $cb = _subname any_tail => $_[0];
1090 6 my ( $deadline, @lambdas) = context;
1091 6 my $n = $#lambdas;
1092 6 ▶ return _empty(any_tail => \&any_tail, $cb) unless @lambdas;
Branch: 1 branch
BranchTrueFalseTotal
unless @lambdas 0 6 6
1093
1094 6 my ( @ret, @watchers);
1095 6 my $timer;
1096
1097 3 ▶ $timer = $THIS-> watch_timer( $deadline, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1098 3 local *__ANON__ = "IO::Lambda::any_tail::callback1";
1099 3 $THIS = shift;
1100 3 @CONTEXT = ($deadline, @lambdas);
1101 3 $METHOD = \&any_tail;
1102 3 $CALLBACK = $cb;
1103 3 ▶ @ret = $cb-> (@ret) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 3 0 3
1104 3 $THIS-> cancel_event($_) for @watchers;
1105 3 return @ret;
1106 6 ▶ }) if defined $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if defined $deadline 6 0 6
1107
1108 6 my $watcher;
1109 4 ▶ $watcher = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1110 4 push @ret, shift;
1111 4 ▶ return if $n--;
Branch: 1 branch
BranchTrueFalseTotal
if $n-- 3 1 4
1112
1113 1 local *__ANON__ = "IO::Lambda::any_tail::callback2";
1114 1 $THIS = shift;
1115 1 @CONTEXT = ($deadline, @lambdas);
1116 1 $METHOD = \&any_tail;
1117 1 $CALLBACK = $cb;
1118 1 ▶ @ret = $cb-> (@ret) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 1 0 1
1119 1 ▶ $THIS-> cancel_event( $timer) if $timer;
Branch: 1 branch
BranchTrueFalseTotal
if $timer 1 0 1
1120 1 return @ret;
1121 6 };
1122
1123 @watchers = map {
1124 6 my $l = $_;
1125 4 ▶ $THIS-> watch_lambda( $l, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1126 4 $watcher->($l, @_);
1127 })
1128 10 } @lambdas;
1129 }
1130
1131 #
1132 # Part III - High order lambdas
1133 #
1134 ################################################################
1135
1136 # fold($l) :: @b -> @c
1137 # $l :: ($a,@b) -> @c
1138 sub fold($)
1139 1 ▶{
Subroutine fold: called CC=1 SCAR=0.0
Pod documented
1140 1 my $l = shift;
1141 3 ▶ lambda {
Subroutine __ANON__: called CC=1 SCAR=0.0
1142 3 my @q = @_;
1143 3 context $l, shift(@q), shift(@q);
1144 10 ▶ tail {
Subroutine __ANON__: called CC=2 SCAR=6.9
1145 10 ▶ return @_ unless @q;
Branch: 1 branch
BranchTrueFalseTotal
unless @q 3 7 10
1146 7 context $l, shift(@q), @_;
1147 7 again;
1148 3 }}
1149 1 }
1150
1151 # mapcar($l) :: (@p) -> @r
1152 sub mapcar($)
1153 2 ▶{
Subroutine mapcar: called CC=1 SCAR=0.0
Pod documented
1154 2 my $lambda = shift;
1155 3 ▶ lambda {
Subroutine __ANON__: called CC=2 SCAR=6.9
1156 3 my @ret;
1157 3 my @p = @_;
1158 3 ▶ return unless @p;
Branch: 1 branch
BranchTrueFalseTotal
unless @p 0 3 3
1159 3 context $lambda, shift @p;
1160 15 ▶ tail {
Subroutine __ANON__: called CC=2 SCAR=6.9
1161 15 push @ret, @_;
1162 15 ▶ return @ret unless @p;
Branch: 1 branch
BranchTrueFalseTotal
unless @p 3 12 15
1163 12 context $lambda, shift @p;
1164 12 again;
1165 3 }}
1166 2 }
1167
1168 # filter($l) :: (@p) -> @r
1169 sub filter($)
1170 1 ▶{
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
1171 1 my $lambda = shift;
1172 1 ▶ lambda {
Subroutine __ANON__: called CC=2 SCAR=6.9
1173 1 my @ret;
1174 1 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
1175 1 my @p = @_;
1176 1 my $p = shift @p;
1177 1 context $lambda, $p;
1178 5 ▶ tail {
Subroutine __ANON__: called CC=3 SCAR=11.0
1179 5 ▶ push @ret, $p if shift;
Branch: 1 branch
BranchTrueFalseTotal
if shift() 3 2 5
1180 5 ▶ return @ret unless @p;
Branch: 1 branch
BranchTrueFalseTotal
unless @p 1 4 5
1181 4 $p = shift @p;
1182 4 context $lambda, $p;
1183 4 again;
1184 1 }}
1185 1 }
1186
1187 # curry(@a -> $l) :: @a -> @b
1188 sub curry(&)
1189 3 ▶{
Subroutine curry: called CC=1 SCAR=0.0
Pod documented
1190 3 my $cb = $_[0];
1191 12 ▶ lambda {
Subroutine __ANON__: called CC=1 SCAR=0.0
1192 12 context $cb->(@_);
1193 12 &tail();
1194 }
1195 3 }
1196
1197 # seq() :: (@l) -> @m
1198 10 ▶sub seq { mapcar curry { shift } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine seq: called CC=1 SCAR=0.0
Pod documented
1199
1200 # par($max = 0) :: (@l) -> @m
1201 sub par
1202 1 ▶{
Subroutine par: called CC=2 SCAR=7.1
Pod documented
1203 1 ▶ my $max = $_[0] || 0;
Condition50%1/2
$_[0] || 0
l!l
10
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
1204
1205 1 ▶ lambda {
Subroutine __ANON__: called CC=3 SCAR=11.1
1206 1 my @q = @_;
1207 1 my @ret;
1208 1 ▶ $max = @q if $max < 1 or $max > @q;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 1 or $max > @q 0 1 1
Condition33%1/3
$max < 1 or $max > @q
l!l&&r!l&&!r
001
MC/DC0%0/2
$max < 1 or $max > @q
$max < 1 $max > @q
Truth table33%1/3
A or B
A= $max < 1B= $max > @q
ABresult
1X1
011
000
1209 context map {
1210 1 ▶ lambda {
Subroutine __ANON__: called CC=2 SCAR=6.9
1211 3 ▶ return unless @q;
Branch: 1 branch
BranchTrueFalseTotal
unless @q 0 3 3
1212 3 context shift @q;
1213 9 ▶ tail {
Subroutine __ANON__: called CC=2 SCAR=6.9
1214 9 push @ret, @_;
1215 9 ▶ return unless @q;
Branch: 1 branch
BranchTrueFalseTotal
unless @q 3 6 9
1216 6 context shift @q;
1217 6 again;
1218 3 }}
1219 3 } 1 .. $max;
1220 1 ▶ tails { @ret }
Subroutine __ANON__: called CC=1 SCAR=0.0
1221 1 }
1222 1 }
1223
1224
1225 # sysread lambda wrapper
1226 #
1227 # ioresult :: ($result, $error)
1228 # sysreader() :: ($fh, $buf, $length, $deadline) -> ioresult
1229 38 ▶sub sysreader (){ lambda
Subroutine sysreader: called CC=1 SCAR=1.3
Pod documented
1230 71 ▶{
Subroutine __ANON__: called CC=2 SCAR=9.4
1231 71 my ( $fh, $buf, $length, $deadline) = @_;
1232 71 ▶ $$buf = '' unless defined $$buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$buf 0 71 71
1233
1234 67 ▶ this-> watch_io( IO_READ, $fh, $deadline, subname _sysreader => sub {
Subroutine __ANON__: called CC=8 SCAR=31.1
1235 67 ▶ return undef, 'timeout' unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 67 67
1236 67 local $SIG{PIPE} = 'IGNORE';
1237 67 my $n = sysread( $fh, $$buf, $length, length($$buf));
1238 67 ▶ if ( $DEBUG_IO) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::DEBUG_IO) 0 67 67
1239 0 ▶ warn "fh(", fileno($fh), ") read ", ( defined($n) ? "$n bytes" : "error $!"), "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 0 0 0
1240 0 ▶ warn substr( $$buf, length($$buf) - $n), "\n" if $DEBUG_IO > 1 and ($n || 0) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_IO > 1 and ($n || 0) > 0 0 0 0
Condition0%0/5
$IO::Lambda::DEBUG_IO > 1 and ($n || 0) > 0
!ll&&!rl&&r
000
$n || 0
l!l
00
MC/DC0%0/2
$IO::Lambda::DEBUG_IO > 1 and ($n || 0) > 0
$IO::Lambda::DEBUG_IO > 1 $n
Truth table0%0/3
A and B
A= $IO::Lambda::DEBUG_IO > 1B= $n
ABresult
0X0
100
111
1241 }
1242 67 ▶ return undef, $! unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 67 67
1243 67 return $n;
1244 })
1245 38 }}
1246
1247 # syswrite() lambda wrapper
1248 #
1249 # syswriter() :: ($fh, $buf, $length, $offset, $deadline) -> ioresult
1250 0 ▶sub syswriter (){ lambda
Subroutine syswriter: not called CC=1 SCAR=6.9
Pod documented
1251 0 ▶{
Subroutine __ANON__: not called CC=1 SCAR=6.9
1252 0 my ( $fh, $buf, $length, $offset, $deadline) = @_;
1253
1254 0 ▶ this-> watch_io( IO_WRITE, $fh, $deadline, subname _syswriter => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
1255 0 ▶ return undef, 'timeout' unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
1256 0 local $SIG{PIPE} = 'IGNORE';
1257 0 my $n = syswrite( $fh, $$buf, $length, $offset);
1258 0 ▶ if ( $DEBUG_IO) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Lambda::DEBUG_IO) 0 0 0
1259 0 ▶ warn "fh(", fileno($fh), ") wrote ", ( defined($n) ? "$n bytes out of $length" : "error $!"), "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 0 0 0
1260 0 ▶ warn substr( $$buf, $offset, $n), "\n" if $DEBUG_IO > 1 and ($n || 0) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_IO > 1 and ($n || 0) > 0 0 0 0
Condition0%0/5
$IO::Lambda::DEBUG_IO > 1 and ($n || 0) > 0
!ll&&!rl&&r
000
$n || 0
l!l
00
MC/DC0%0/2
$IO::Lambda::DEBUG_IO > 1 and ($n || 0) > 0
$IO::Lambda::DEBUG_IO > 1 $n
Truth table0%0/3
A and B
A= $IO::Lambda::DEBUG_IO > 1B= $n
ABresult
0X0
100
111
1261 }
1262 0 ▶ return undef, $! unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
1263 0 return $n;
1264 0 });
1265 0 }}
1266
1267 sub _match
1268 76 ▶{
Subroutine _match: called CC=4 SCAR=17.1
1269 76 my ( $cond, $buf) = @_;
1270
1271 76 ▶ return unless defined $cond;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cond 71 5 76
1272
1273 5 ▶ return ($$buf =~ /($cond)/)[0] if ref($cond) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cond eq "Regexp" 5 0 5
1274 0 ▶ return $cond->($buf) if ref($cond) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cond eq "CODE" 0 0 0
1275 0 return length($$buf) >= $cond;
1276 }
1277
1278 # read from stream until condition is met
1279 #
1280 # readbuf($reader) :: ($fh, $$buf, $cond, $deadline) -> ioresult
1281 sub readbuf
1282 41 ▶{
Subroutine readbuf: called CC=2 SCAR=7.0
Pod documented
1283 41 ▶ my $reader = shift || sysreader;
Condition66%2/3
shift() || &sysreader()
l!l&&r!l&&!r
3380
MC/DC0%0/2
shift() || &sysreader()
shift() &sysreader()
Truth table66%2/3
A || B
A= shift()B= &sysreader()
ABresult
1X1
011
000
1284
1285 41 ▶ lambda {
Subroutine __ANON__: called CC=7 SCAR=19.8
1286 41 my ( $fh, $buf, $cond, $deadline) = @_;
1287
1288 41 ▶ $$buf = "" unless defined $$buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$buf 2 39 41
1289
1290 41 my $match = _match( $cond, $buf);
1291 41 ▶ return $match if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 1 40 41
1292
1293 40 my ($maxbytes, $bufsize);
1294 40 ▶ $maxbytes = $cond - length($$buf)
Branch: 1 branch
BranchTrueFalseTotal
if defined $cond and not ref $cond and $cond > length $$buf 0 40 40
Condition50%3/6
defined $cond and not ref $cond and $cond > length $$buf
!ll&&!rl&&r
4000
defined $cond and not ref $cond
!ll&&!rl&&r
3820
MC/DC0%0/3
defined $cond and not ref $cond and $cond > length $$buf
defined $cond not ref $cond $cond > length $$buf
Truth table50%2/4
A and not(B) and C
A= defined $condB= not ref $condC= $cond > length $$buf
ABCresult
0XX0
10X0
1100
1111
1295 if defined($cond) and not ref($cond) and $cond > length($$buf);
1296 40 ▶ $bufsize = defined($maxbytes) ? $maxbytes : 65536;
Branch: 1 branch
BranchTrueFalseTotal
defined $maxbytes ? : 0 40 40
1297
1298 40 my $savepos = pos($$buf); # useful when $cond is a regexp
1299
1300 40 context $reader, $fh, $buf, $bufsize, $deadline;
1301 69 ▶ tail {
Subroutine __ANON__: called CC=6 SCAR=18.1
1302 69 pos($$buf) = $savepos;
1303
1304 69 my $bytes = shift;
1305 69 ▶ return undef, shift unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 1 68 69
1306
1307 68 ▶ unless ( $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes) 33 35 68
1308 33 ▶ return 1 unless defined $cond;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cond 33 0 33
1309 0 return undef, 'eof';
1310 }
1311
1312 # got line? return it
1313 35 my $match = _match( $cond, $buf);
1314 35 ▶ return $match if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 2 33 35
1315
1316 # otherwise, just wait for more data
1317 33 ▶ $bufsize -= $bytes if defined $maxbytes;
Branch: 1 branch
BranchTrueFalseTotal
if defined $maxbytes 0 33 33
1318
1319 33 context $reader, $fh, $buf, $bufsize, $deadline;
1320 33 again;
1321 40 }}
1322 41 }
1323
1324 # curry readbuf()
1325 #
1326 # getline($reader) :: ($fh, $$buf, $deadline) -> ioresult
1327 sub getline
1328 1 ▶{
Subroutine getline: called CC=1 SCAR=0.0
Pod documented
1329 1 my $reader = shift;
1330 1 ▶ lambda {
Subroutine __ANON__: called CC=2 SCAR=7.1
1331 1 my ( $fh, $buf, $deadline) = @_;
1332 1 ▶ confess "getline() needs a buffer! ( f.ex getline,\$fh,\\(my \$buf='') )"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $buf 0 1 1
1333 unless ref($buf);
1334 1 context readbuf($reader), $fh, $buf, qr/^[^\n]*\n/, $deadline;
1335 1 ▶ tail {
Subroutine __ANON__: called CC=2 SCAR=7.2
1336 1 ▶ substr( $$buf, 0, length($_[0]), '') unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 1 0 1
1337 1 @_;
1338 1 }}
1339 1 }
1340
1341 # write whole buffer to stream
1342 #
1343 # writebuf($writer) :: syswriter
1344 sub writebuf
1345 2 ▶{
Subroutine writebuf: called CC=2 SCAR=7.0
Pod documented
1346 2 ▶ my $writer = shift || syswriter;
Condition33%1/3
shift() || &syswriter()
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || &syswriter()
shift() &syswriter()
Truth table33%1/3
A || B
A= shift()B= &syswriter()
ABresult
1X1
011
000
1347
1348 2 ▶ lambda {
Subroutine __ANON__: called CC=4 SCAR=14.0
1349 2 my ( $fh, $buf, $len, $offs, $deadline) = @_;
1350
1351 2 my ( $written, $recheck_length, $olen) = (0);
1352 2 ▶ $$buf = "" unless defined $$buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$buf 0 2 2
1353 2 ▶ $offs = 0 unless defined $offs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offs 2 0 2
1354 2 ▶ unless ( defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $len) 2 0 2
1355 2 $olen = $len = length $$buf;
1356 2 $recheck_length++;
1357 }
1358
1359 2 context $writer, $fh, $buf, $len, $offs, $deadline;
1360 13 ▶ tail {
Subroutine __ANON__: called CC=5 SCAR=16.1
1361 13 my $bytes = shift;
1362 13 ▶ return undef, shift unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 13 13
1363
1364 13 $offs += $bytes;
1365 13 $written += $bytes;
1366 13 $len -= $bytes;
1367 13 ▶ if ( $recheck_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recheck_length) 13 0 13
1368 13 my $l = length $$buf;
1369 13 ▶ if ( $l > $olen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $olen) 1 12 13
1370 1 $len += $l - $olen;
1371 1 $olen = $l;
1372 }
1373 }
1374 13 ▶ return $written if $len <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 0 2 11 13
1375
1376 11 context $writer, $fh, $buf, $len, $offs, $deadline;
1377 11 again;
1378 2 }}
1379 2 }
1380
1381 #
1382 # Part IV - Developer API for custom condvars and event loops
1383 #
1384 ################################################################
1385
1386 # register condvar listener
1387 sub bind
1388 52 ▶{
Subroutine bind: called CC=2 SCAR=7.0
Pod documented
1389 52 my $self = shift;
1390
1391 # create new condition
1392 52 ▶ confess "can't register events on a stopped lambda" if $self-> {stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 52 52
1393
1394 52 my $rec = [ $self, @_ ];
1395 52 push @{$self-> {in}}, $rec;
1396
1397 52 return $rec;
1398 }
1399
1400 # stop listening on a condvar
1401 sub resolve
1402 42 ▶{
Subroutine resolve: called CC=5 SCAR=16.8
Pod documented
1403 42 my ( $self, $rec) = @_;
1404
1405 42 my $in = $self-> {in};
1406 42 my $nn = @$in;
1407 42 @$in = grep { $rec != $_ } @$in;
1408 42 ▶ die _d($self, "stray condvar event $rec (@$rec)")
Branch: 1 branch
BranchTrueFalseTotal
if $nn == @$in or $self != $rec->[0] 0 42 42
Condition33%1/3
$nn == @$in or $self != $rec->[0]
l!l&&r!l&&!r
0042
MC/DC0%0/2
$nn == @$in or $self != $rec->[0]
$nn == @$in $self != $rec->[0]
Truth table33%1/3
A or B
A= $nn == @$inB= $self != $rec->[0]
ABresult
1X1
011
000
1409 if $nn == @$in or $self != $rec->[WATCH_OBJ];
1410
1411 42 undef $rec-> [WATCH_OBJ]; # unneeded references
1412
1413 42 ▶ unless ( @$in) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$in) 42 0 42
1414 42 ▶ warn _d( $self, 'stopped') if $DEBUG_LAMBDA;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::DEBUG_LAMBDA 0 42 42
1415 42 $self-> {stopped} = 1;
1416 42 delete $self->{frames};
1417 }
1418 }
1419
1420 sub callout
1421 6 ▶{
Subroutine callout: called CC=2 SCAR=6.9
Pod undocumented
1422 6 my ( $self, $cb, @param) = @_;
1423 6 ▶ @{$self->{last}} = $cb ? $cb-> (@param) : @param;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 2 4 6
1424 }
1425
1426 16 ▶sub add_loop { push @LOOPS, shift }
Subroutine add_loop: called CC=1 SCAR=0.0
Pod undocumented
1427 16 ▶sub remove_loop { @LOOPS = grep { defined and $_ != $_[0] } @LOOPS }
Subroutine remove_loop: called CC=2 SCAR=7.1
Pod undocumented
Condition66%2/3
defined $_ && $_ != $_[0]
!ll&&!rl&&r
0161
MC/DC50%1/2
defined $_ && $_ != $_[0]
defined $_ $_ != $_[0]
Truth table66%2/3
A && B
A= defined $_B= $_ != $_[0]
ABresult
0X0
100
111
1428
1429 0 ▶sub __end { clear(); undef %EVENTS; undef @LOOPS; } # for threads
Subroutine __end: not called CC=1 SCAR=6.9
1430
1431 package IO::Lambda::Loop;
1432 30 ▶use vars qw($DEFAULT);
Subroutine BEGIN: called CC=1 SCAR=0.0
1433 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1434 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1435
1436 30 ▶$DEFAULT = 'Select' unless defined $DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $IO::Lambda::Loop::DEFAULT 30 0 30
1437 28 ▶sub default { $DEFAULT = shift }
Subroutine default: called CC=1 SCAR=0.0
1438
1439 sub new
1440 27 ▶{
Subroutine new: called CC=4 SCAR=14.4
1441 27 ▶ return $IO::Lambda::LOOP if $IO::Lambda::LOOP;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::LOOP 0 27 27
1442
1443 27 my ( $class, %opt) = @_;
1444
1445 27 ▶ $opt{type} ||= $DEFAULT;
Condition33%1/3
$opt{"type"} ||= $IO::Lambda::Loop::DEFAULT
l!l&&r!l&&!r
0270
MC/DC0%0/2
$opt{"type"} ||= $IO::Lambda::Loop::DEFAULT
$opt{"type"} $IO::Lambda::Loop::DEFAULT
Truth table33%1/3
A ||= B
A= $opt{"type"}B= $IO::Lambda::Loop::DEFAULT
ABresult
1X1
011
000
1446 27 $class .= "::$opt{type}";
1447 27 ▶ eval "use $class;";
Subroutine BEGIN: called CC=1 SCAR=0.0
1448 27 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 27 27
1449
1450 27 return $IO::Lambda::LOOP = $class-> new();
1451 }
1452
1453 30 1;
1454
1455 __DATA__
« Lambda/DNS.pm ↑ Summary Lambda/HTTP/UserAgent.pm »