Lambda.pm

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