Future/PP.pm

filter: Statementstmt 97.5% 471 / 483 Branchbran 89.9% 304 / 338 Conditioncond 81.9% 150 / 183 MC/DCmcdc 60.9% 67 / 110 Subroutinesub 96.2% 76 / 79 Podpod 0.0% 0 / 48 totaltotal 86.0% 1068 / 1241 CC 249 SCAR 24.2
CC
249
Cov
92%
_mark_ready
38.1
_sequence
21.1
needs_any
16.0
CRAP 11.2
↑ Summary Future/Utils.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2011-2026 -- leonerd@leonerd.org.uk
5  
6 package Future::PP 0.54;
7  
8 33 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶no warnings 'recursion'; # Disable the "deep recursion" warning
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 33 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 33 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 33 our @ISA = qw( Future::_base );
16  
17 33 ▶use Carp qw(); # don't import croak
Subroutine BEGIN: called CC=1 SCAR=0.0
18 33 ▶use List::Util 1.29 qw( pairs pairkeys );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 33 ▶use Scalar::Util qw( weaken blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 33 ▶use Time::HiRes qw( gettimeofday );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 33 our @CARP_NOT = qw( Future Future::_base Future::Utils );
23  
24 33 ▶use constant DEBUG => !!$ENV{PERL_FUTURE_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 33 ▶use constant STRICT => !!$ENV{PERL_FUTURE_STRICT};
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # Callback flags
29 33 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 33 CB_DONE => 1<<0, # Execute callback on done
31 CB_FAIL => 1<<1, # Execute callback on fail
32 CB_CANCEL => 1<<2, # Execute callback on cancellation
33  
34 CB_SELF => 1<<3, # Pass $self as first argument
35 CB_RESULT => 1<<4, # Pass result/failure as a list
36  
37 CB_SEQ_ONDONE => 1<<5, # Sequencing on success (->then)
38 CB_SEQ_ONFAIL => 1<<6, # Sequencing on failure (->else)
39  
40 CB_SEQ_IMDONE => 1<<7, # $code is in fact immediate ->done result
41 CB_SEQ_IMFAIL => 1<<8, # $code is in fact immediate ->fail result
42  
43 CB_SEQ_STRICT => 1<<9, # Complain if $code didn't return a Future
44 33 };
45  
46 33 ▶use constant CB_ALWAYS => CB_DONE|CB_FAIL|CB_CANCEL;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 6 ▶sub _shortmess ( $msg )
Subroutine _shortmess: called CC=1 SCAR=0.0
49 {
50 6 my $at = Carp::shortmess( $msg );
51 6 chomp $at; $at =~ s/\.$//;
52 6 return $at;
53 }
54  
55 486 ▶sub _callable ( $cb )
Subroutine _callable: called CC=3 SCAR=15.1
56 {
57 486 ▶ defined $cb and ( reftype($cb) eq 'CODE' || overload::Method($cb, '&{}') );
Branch: 1 branch
BranchTrueFalseTotal
defined $cb and builtin::reftype($cb) eq "CODE" || overload::Method($cb, "&{}") 486 0 486
Condition33%2/6
defined $cb && (builtin::reftype($cb) eq 'CODE' || overload::Method($cb, '&{}'))
!ll&&!rl&&r
00486
builtin::reftype($cb) eq "CODE" or overload::Method($cb, "&{}")
l!l&&r!l&&!r
48600
MC/DC0%0/3
defined $cb && (builtin::reftype($cb) eq 'CODE' || overload::Method($cb, '&{}'))
defined $cb builtin::reftype($cb) eq "CODE" overload::Method($cb, "&{}")
Truth table25%1/4
A && B or C
A= defined $cbB= builtin::reftype($cb) eq "CODE"C= overload::Method($cb, "&{}")
ABCresult
0XX0
1000
11X1
1011
58 }
59  
60 721 ▶sub new ( $proto )
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
61 {
62 return bless {
63 ready => 0,
64 callbacks => [], # [] = [$type, ...]
65 ( DEBUG ?
66 721 ▶ ( do { my $at = Carp::shortmess( "constructed" );
Branch: 1 branch
BranchTrueFalseTotal
$Future::TIMES ? : 8 713 721
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1405810
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
67 chomp $at; $at =~ s/\.$//;
68 constructed_at => $at } )
69 : () ),
70 ( $Future::TIMES ?
71 ( btime => [ gettimeofday ] )
72 : () ),
73 }, ( ref $proto || $proto );
74 }
75  
76 12 ▶sub __selfstr ( $self )
Subroutine __selfstr: called CC=3 SCAR=11.0
77 {
78 20 my $str = "$self";
79 20 ▶ $str .= " (\"$self->{label}\")" if defined $self->{label};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"label"} 3 9 12
80 20 ▶ $str .= " ($self->{constructed_at})" if defined $self->{constructed_at};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"constructed_at"} 4 8 12
81 20 return $str;
82 }
83  
84 33 my $GLOBAL_END;
85 33 ▶END { $GLOBAL_END = 1; }
Subroutine END: called CC=1 SCAR=0.0
86  
87 8 ▶sub DESTROY_debug ( $self ) {
Subroutine DESTROY_debug: called CC=8 SCAR=21.1
Pod undocumented
88 8 ▶ return if $GLOBAL_END;
Branch: 1 branch
BranchTrueFalseTotal
if $GLOBAL_END 0 8 8
89 8 ▶ return if $self->{ready} and ( $self->{reported} or !$self->{failure} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ready"} and $self->{"reported"} || !$self->{"failure"} 5 3 8
Condition83%5/6
$self->{"ready"} and $self->{"reported"} || !$self->{"failure"}
!ll&&!rl&&r
215
$self->{"reported"} or not $self->{"failure"}
l!l&&r!l&&!r
051
MC/DC66%2/3
$self->{"ready"} and $self->{"reported"} || !$self->{"failure"}
$self->{"ready"} $self->{"reported"} not $self->{"failure"}
Truth table75%3/4
A and B or not(C)
A= $self->{"ready"}B= $self->{"reported"}C= not $self->{"failure"}
ABCresult
0XX0
1000
11X1
1011
90  
91 3 my $lost_at = join " line ", (caller)[1,2];
92 # We can't actually know the real line where the last reference was lost;
93 # a variable set to 'undef' or close of scope, because caller can't see it;
94 # the current op has already been updated. The best we can do is indicate
95 # 'near'.
96  
97 3 ▶ if( $self->{ready} and $self->{failure} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'ready'} and $self->{'failure'}) { } 1 2 3
elsif (not $self->{'ready'}) { } 2 0 2
Condition66%2/3
$self->{"ready"} and $self->{"failure"}
!ll&&!rl&&r
201
MC/DC50%1/2
$self->{"ready"} and $self->{"failure"}
$self->{"ready"} $self->{"failure"}
Truth table66%2/3
A and B
A= $self->{"ready"}B= $self->{"failure"}
ABresult
0X0
100
111
98 1 warn "${\$self->__selfstr} was lost near $lost_at with an unreported failure of: " .
99 1 $self->{failure}[0] . "\n";
100 }
101 elsif( !$self->{ready} ) {
102 2 warn "${\$self->__selfstr} was lost near $lost_at before it was ready.\n";
103 }
104 }
105 33 *DESTROY = \&DESTROY_debug if DEBUG;
106  
107 477 ▶sub is_ready ( $self )
Subroutine is_ready: called CC=1 SCAR=0.0
Pod undocumented
108 {
109 477 return $self->{ready};
110 }
111  
112 79 ▶sub is_done ( $self )
Subroutine is_done: called CC=3 SCAR=11.1
Pod undocumented
113 {
114 79 ▶ return $self->{ready} && !$self->{failure} && !$self->{cancelled};
Condition83%5/6
$self->{'ready'} && !$self->{'failure'} && !$self->{'cancelled'}
!ll&&!rl&&r
17062
$self->{"ready"} and not $self->{"failure"}
!ll&&!rl&&r
21562
MC/DC66%2/3
$self->{'ready'} && !$self->{'failure'} && !$self->{'cancelled'}
$self->{"ready"} not $self->{"failure"} !$self->{'cancelled'}
Truth table75%3/4
A and not(B) && not(C)
A= $self->{"ready"}B= not $self->{"failure"}C= !$self->{'cancelled'}
ABCresult
0XX0
10X0
1100
1111
115 }
116  
117 9 ▶sub is_failed ( $self )
Subroutine is_failed: called CC=2 SCAR=6.9
Pod undocumented
118 {
119 9 ▶ return $self->{ready} && !!$self->{failure}; # boolify
Condition100%3/3
$self->{'ready'} && !!$self->{'failure'}
!ll&&!rl&&r
216
MC/DC100%2/2
$self->{'ready'} && !!$self->{'failure'}
$self->{'ready'} !!$self->{'failure'}
Truth table100%3/3
A && B
A= $self->{'ready'}B= !!$self->{'failure'}
ABresult
0X0
100
111
120 }
121  
122 38 ▶sub is_cancelled ( $self )
Subroutine is_cancelled: called CC=1 SCAR=0.0
Pod undocumented
123 {
124 38 return $self->{cancelled};
125 }
126  
127 8 ▶sub state ( $self )
Subroutine state: called CC=4 SCAR=13.9
Pod undocumented
128 {
129 return !$self->{ready} ? "pending" :
130 DEBUG ? $self->{ready_at} :
131 $self->{failure} ? "failed" :
132 8 ▶ $self->{cancelled} ? "cancelled" :
Branch: 3 branches
BranchTrueFalseTotal
!$self->{'ready'} ? : 1 7 8
$self->{'failure'} ? : 3 4 7
$self->{'cancelled'} ? : 1 3 4
133 "done";
134 }
135  
136 561 ▶sub _mark_ready ( $self, $reason )
Subroutine _mark_ready: called CC=38 SCAR=36.4
137 {
138 561 $self->{ready} = 1;
139 561 $self->{ready_at} = _shortmess $reason if DEBUG;
140  
141 561 ▶ if( $Future::TIMES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Future::TIMES) 4 557 561
142 4 $self->{rtime} = [ gettimeofday ];
143 }
144  
145 561 delete $self->{on_cancel};
146 561 ▶ $_->[0] and $_->[0]->_revoke_on_cancel( $_->[1] ) for $self->{revoke_when_ready}->@*;
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] and $_->[0]->_revoke_on_cancel($_->[1]) 192 3 195
147 561 delete $self->{revoke_when_ready};
148  
149 561 ▶ my $callbacks = delete $self->{callbacks} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $callbacks = delete $self->{"callbacks"} or return 556 5 561
150  
151 556 my $cancelled = $self->{cancelled};
152 556 my $fail = defined $self->{failure};
153 556 ▶ my $done = !$fail && !$cancelled;
Condition100%3/3
$fail || $cancelled
l!l&&r!l&&!r
103112341
MC/DC100%2/2
$fail || $cancelled
$fail $cancelled
Truth table100%3/3
A || B
A= $failB= $cancelled
ABresult
1X1
011
000
154  
155 my @result = $done ? $self->{result}->@* :
156 $fail ? $self->{failure}->@* :
157 556 ▶ ();
Branch: 2 branches
BranchTrueFalseTotal
$done ? : 341 215 556
$fail ? : 103 112 215
158  
159 556 foreach my $cb ( @$callbacks ) {
160 341 my ( $flags, $code ) = @$cb;
161 341 ▶ my $is_future = blessed( $code ) && $code->isa( "Future" );
Condition66%2/3
builtin::blessed($code) && $code->isa('Future')
!ll&&!rl&&r
278063
MC/DC50%1/2
builtin::blessed($code) && $code->isa('Future')
builtin::blessed($code) $code->isa('Future')
Truth table66%2/3
A && B
A= builtin::blessed($code)B= $code->isa('Future')
ABresult
0X0
100
111
162  
163 341 ▶ next if $done and not( $flags & CB_DONE );
Branch: 1 branch
BranchTrueFalseTotal
if $done and not $flags & 1 40 301 341
Condition100%3/3
$done and not $flags & 1
!ll&&!rl&&r
12517640
MC/DC100%2/2
$done and not $flags & 1
$done not $flags & 1
Truth table100%3/3
A and not(B)
A= $doneB= not $flags & 1
ABresult
0X0
100
111
164 301 ▶ next if $fail and not( $flags & CB_FAIL );
Branch: 1 branch
BranchTrueFalseTotal
if $fail and not $flags & 2 3 298 301
Condition100%3/3
$fail and not $flags & 2
!ll&&!rl&&r
250483
MC/DC100%2/2
$fail and not $flags & 2
$fail not $flags & 2
Truth table100%3/3
A and not(B)
A= $failB= not $flags & 2
ABresult
0X0
100
111
165 298 ▶ next if $cancelled and not( $flags & CB_CANCEL );
Branch: 1 branch
BranchTrueFalseTotal
if $cancelled and not $flags & 4 32 266 298
Condition100%3/3
$cancelled and not $flags & 4
!ll&&!rl&&r
2244232
MC/DC100%2/2
$cancelled and not $flags & 4
$cancelled not $flags & 4
Truth table100%3/3
A and not(B)
A= $cancelledB= not $flags & 4
ABresult
0X0
100
111
166  
167 266 ▶ $self->{reported} = 1 if $fail;
Branch: 1 branch
BranchTrueFalseTotal
if $fail 48 218 266
168  
169 266 ▶ if( $is_future ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_future) { } 23 243 266
elsif ($flags & 96) { } 60 183 243
170 23 ▶ $done ? $code->done( @result ) :
Branch: 2 branches
BranchTrueFalseTotal
$done ? : 16 7 23
$fail ? : 6 1 7
171 $fail ? $code->fail( @result ) :
172 $code->cancel;
173 }
174 elsif( $flags & (CB_SEQ_ONDONE|CB_SEQ_ONFAIL) ) {
175 60 my ( undef, undef, $fseq ) = @$cb;
176 60 ▶ if( !$fseq ) { # weaken()ed; it might be gone now
Branch: 1 branch
BranchTrueFalseTotal
unless ($fseq) 2 58 60
177 # This warning should always be printed, even not in DEBUG mode.
178 # It's always an indication of a bug
179 2 Carp::carp "${\$self->__selfstr} lost a sequence Future";
180 2 next;
181 }
182  
183 58 my $f2;
184 58 ▶ if( $done and $flags & CB_SEQ_ONDONE or
Branch: 1 branch
BranchTrueFalseTotal
if ($done and $flags & 32 or $fail and $flags & 64) { } 52 6 58
Condition100%9/9
$done and $flags & 32 or $fail and $flags & 64
l!l&&r!l&&!r
34186
$done and $flags & 32
!ll&&!rl&&r
21334
$fail and $flags & 64
!ll&&!rl&&r
3318
MC/DC75%3/4
$done and $flags & 32 or $fail and $flags & 64
$done $flags & 32 $fail $flags & 64
Truth table57%4/7
A and B or C and D
A= $doneB= $flags & 32C= $failD= $flags & 64
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
185 $fail and $flags & CB_SEQ_ONFAIL ) {
186  
187 52 ▶ if( $flags & CB_SEQ_IMDONE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($flags & 128) { } 2 50 52
elsif ($flags & 256) { } 2 48 50
188 2 $fseq->done( @$code );
189 2 next;
190 }
191 elsif( $flags & CB_SEQ_IMFAIL ) {
192 2 $fseq->fail( @$code );
193 2 next;
194 }
195  
196 48 ▶ my @args = (
Branch: 2 branches
BranchTrueFalseTotal
$flags & 8 ? : 22 26 48
$flags & 16 ? : 28 20 48
197 ( $flags & CB_SELF ? $self : () ),
198 ( $flags & CB_RESULT ? @result : () ),
199 );
200  
201 48 ▶ unless( eval { $f2 = $code->( @args ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $f2 = &$code(@args); 1 } }) 4 44 48
202 4 $fseq->fail( $@ );
203 4 next;
204 }
205  
206 44 ▶ unless( blessed $f2 and $f2->isa( "Future" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($f2) and $f2->isa("Future")) 3 41 44
Condition66%2/3
builtin::blessed($f2) and $f2->isa("Future")
!ll&&!rl&&r
3041
MC/DC50%1/2
builtin::blessed($f2) and $f2->isa("Future")
builtin::blessed($f2) $f2->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($f2)B= $f2->isa("Future")
ABresult
0X0
100
111
207 # Upgrade a non-Future result, or complain in strict mode
208 3 ▶ if( $flags & CB_SEQ_STRICT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 512) 0 3 3
209 0 $fseq->fail( "Expected " . Future::CvNAME_FILE_LINE($code) . " to return a Future" );
210 0 next;
211 }
212 3 $f2 = Future->done( $f2 );
213 }
214  
215 44 $fseq->on_cancel( $f2 );
216 }
217 else {
218 6 $f2 = $self;
219 }
220  
221 50 ▶ if( $f2->is_ready ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f2->is_ready) { } 30 20 50
222 30 ▶ $f2->on_ready( $fseq ) if !$f2->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
unless $f2->{"cancelled"} 30 0 30
223 }
224 else {
225 20 push $f2->{callbacks}->@*, [ CB_DONE|CB_FAIL, $fseq ];
226 20 weaken( $f2->{callbacks}[-1][1] );
227 }
228 }
229 else {
230 183 ▶ $code->(
Branch: 2 branches
BranchTrueFalseTotal
$flags & 8 ? : 125 58 183
$flags & 16 ? : 58 125 183
231 ( $flags & CB_SELF ? $self : () ),
232 ( $flags & CB_RESULT ? @result : () ),
233 );
234 }
235 }
236 }
237  
238 408 ▶sub done ( $self, @result )
Subroutine done: called CC=6 SCAR=17.9
Pod undocumented
239 {
240 408 ▶ if( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 316 92 408
241 316 ▶ $self->{cancelled} and return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cancelled"} and return $self 3 313 316
242 313 ▶ $self->{ready} and Carp::croak "${\$self->__selfstr} is already ".$self->state." and cannot be ->done";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ready"} and Carp::croak("${\$self->__selfstr;} is already " . $self->state . ' and cannot be ->done') 2 311 313
243 0 ▶ $self->{subs} and Carp::croak "${\$self->__selfstr} is not a leaf Future, cannot be ->done";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"subs"} and Carp::croak("${\$self->__selfstr;} is not a leaf Future, cannot be ->done") 0 311 311
244 311 $self->{result} = [ @result ];
245 311 $self->_mark_ready( "done" );
246 }
247 else {
248 92 $self = $self->new;
249 92 $self->{ready} = 1;
250 92 $self->{ready_at} = _shortmess "done" if DEBUG;
251 92 $self->{result} = [ @result ];
252 92 ▶ if( $Future::TIMES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Future::TIMES) 1 91 92
253 1 $self->{rtime} = [ gettimeofday ];
254 }
255 }
256  
257 403 return $self;
258 }
259  
260 143 ▶sub fail ( $self, $exception, @more )
Subroutine fail: called CC=8 SCAR=20.9
Pod undocumented
261 {
262 143 ▶ if( ref $exception eq "Future::Exception" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exception eq "Future::Exception") 2 141 143
263 2 @more = ( $exception->category, $exception->details );
264 2 $exception = $exception->message;
265 }
266  
267 143 ▶ $exception or Carp::croak "$self ->fail requires an exception that is true";
Branch: 1 branch
BranchTrueFalseTotal
$exception or Carp::croak("$self ->fail requires an exception that is true") 143 0 143
268  
269 143 ▶ if( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 87 56 143
270 87 ▶ $self->{cancelled} and return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cancelled"} and return $self 1 86 87
271 86 ▶ $self->{ready} and Carp::croak "${\$self->__selfstr} is already ".$self->state." and cannot be ->fail'ed";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ready"} and Carp::croak("${\$self->__selfstr;} is already " . $self->state . q[ and cannot be ->fail'ed]) 2 84 86
272 0 ▶ $self->{subs} and Carp::croak "${\$self->__selfstr} is not a leaf Future, cannot be ->fail'ed";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"subs"} and Carp::croak("${\$self->__selfstr;} is not a leaf Future, cannot be ->fail'ed") 0 84 84
273 84 $self->{failure} = [ $exception, @more ];
274 84 $self->_mark_ready( "failed" );
275 }
276 else {
277 56 $self = $self->new;
278 56 $self->{ready} = 1;
279 56 $self->{ready_at} = _shortmess "failed" if DEBUG;
280 56 $self->{failure} = [ $exception, @more ];
281 56 ▶ if( $Future::TIMES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Future::TIMES) 1 55 56
282 1 $self->{rtime} = [ gettimeofday ];
283 }
284 }
285  
286 140 return $self;
287 }
288  
289 338 ▶sub on_cancel ( $self, $code )
Subroutine on_cancel: called CC=6 SCAR=18.1
Pod undocumented
290 {
291 338 ▶ my $is_future = blessed( $code ) && $code->isa( "Future" );
Condition66%2/3
builtin::blessed($code) && $code->isa('Future')
!ll&&!rl&&r
1180220
MC/DC50%1/2
builtin::blessed($code) && $code->isa('Future')
builtin::blessed($code) $code->isa('Future')
Truth table66%2/3
A && B
A= builtin::blessed($code)B= $code->isa('Future')
ABresult
0X0
100
111
292 338 ▶ $is_future or _callable( $code ) or
Branch: 1 branch
BranchTrueFalseTotal
$is_future or _callable($code) or Carp::croak("Expected \$code to be callable or a Future in ->on_cancel") 338 0 338
Condition66%2/3
$is_future or _callable($code)
l!l&&r!l&&!r
2201180
MC/DC0%0/2
$is_future or _callable($code)
$is_future _callable($code)
Truth table66%2/3
A or B
A= $is_futureB= _callable($code)
ABresult
1X1
011
000
293 Carp::croak "Expected \$code to be callable or a Future in ->on_cancel";
294  
295 338 ▶ $self->{ready} and return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ready"} and return $self 23 315 338
296  
297 315 push $self->{on_cancel}->@*, $code;
298 315 ▶ if( $is_future ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_future) 219 96 315
299 219 push $code->{revoke_when_ready}->@*, my $r = [ $self, \$self->{on_cancel}[-1] ];
300 219 weaken( $r->[0] );
301 219 weaken( $r->[1] );
302 }
303  
304 315 return $self;
305 }
306  
307 # An optimised version for Awaitable role
308 0 ▶sub AWAIT_ON_CANCEL ( $self, $code )
Subroutine AWAIT_ON_CANCEL: not called CC=1 SCAR=6.9
Pod undocumented
309 {
310 0 push $self->{on_cancel}->@*, $code;
311 }
312  
313 0 ▶sub AWAIT_CHAIN_CANCEL ( $self, $f2 )
Subroutine AWAIT_CHAIN_CANCEL: not called CC=1 SCAR=6.9
Pod undocumented
314 {
315 0 push $self->{on_cancel}->@*, $f2;
316 0 push $f2->{revoke_when_ready}->@*, my $r = [ $self, \$self->{on_cancel}[-1] ];
317 0 weaken( $r->[0] );
318 0 weaken( $r->[1] );
319 }
320  
321 192 ▶sub _revoke_on_cancel ( $self, $ref )
Subroutine _revoke_on_cancel: called CC=6 SCAR=17.9
322 {
323 192 undef $$ref;
324 192 $self->{empty_on_cancel_slots}++;
325  
326 192 ▶ my $on_cancel = $self->{on_cancel} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $on_cancel = $self->{"on_cancel"} or return 172 20 192
327  
328 # If the list is nontrivally large and over half-empty / under half-full, compact it
329 172 ▶ if( @$on_cancel >= 8 and $self->{empty_on_cancel_slots} >= 0.5 * @$on_cancel ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$on_cancel >= 8 and $self->{"empty_on_cancel_slots"} >= "0.5" * @$on_cancel) 3 169 172
Condition100%3/3
@$on_cancel >= 8 and $self->{"empty_on_cancel_slots"} >= "0.5" * @$on_cancel
!ll&&!rl&&r
81883
MC/DC100%2/2
@$on_cancel >= 8 and $self->{"empty_on_cancel_slots"} >= "0.5" * @$on_cancel
@$on_cancel >= 8 $self->{"empty_on_cancel_slots"} >= "0.5" * @$on_cancel
Truth table100%3/3
A and B
A= @$on_cancel >= 8B= $self->{"empty_on_cancel_slots"} >= "0.5" * @$on_cancel
ABresult
0X0
100
111
330 # We can't grep { defined } because that will break all the existing SCALAR refs
331 3 my $idx = 0;
332 3 ▶ while( $idx < @$on_cancel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @$on_cancel) 175 3 178
333 175 ▶ defined $on_cancel->[$idx] and $idx++, next;
Branch: 1 branch
BranchTrueFalseTotal
defined $on_cancel->[$idx] and ++$idx, next 87 88 175
334 88 splice @$on_cancel, $idx, 1, ();
335 }
336 3 $self->{empty_on_cancel_slots} = 0;
337 }
338 }
339  
340 179 ▶sub on_ready ( $self, $code )
Subroutine on_ready: called CC=10 SCAR=23.1
Pod undocumented
341 {
342 179 ▶ my $is_future = blessed( $code ) && $code->isa( "Future" );
Condition66%2/3
builtin::blessed($code) && $code->isa('Future')
!ll&&!rl&&r
143036
MC/DC50%1/2
builtin::blessed($code) && $code->isa('Future')
builtin::blessed($code) $code->isa('Future')
Truth table66%2/3
A && B
A= builtin::blessed($code)B= $code->isa('Future')
ABresult
0X0
100
111
343 179 ▶ $is_future or _callable( $code ) or
Branch: 1 branch
BranchTrueFalseTotal
$is_future or _callable($code) or Carp::croak("Expected \$code to be callable or a Future in ->on_ready") 179 0 179
Condition66%2/3
$is_future or _callable($code)
l!l&&r!l&&!r
361430
MC/DC0%0/2
$is_future or _callable($code)
$is_future _callable($code)
Truth table66%2/3
A or B
A= $is_futureB= _callable($code)
ABresult
1X1
011
000
344 Carp::croak "Expected \$code to be callable or a Future in ->on_ready";
345  
346 179 ▶ if( $self->{ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ready'}) { } 40 139 179
347 40 my $fail = defined $self->{failure};
348 40 ▶ my $done = !$fail && !$self->{cancelled};
Condition100%3/3
$fail || $self->{'cancelled'}
l!l&&r!l&&!r
11326
MC/DC100%2/2
$fail || $self->{'cancelled'}
$fail $self->{'cancelled'}
Truth table100%3/3
A || B
A= $failB= $self->{'cancelled'}
ABresult
1X1
011
000
349  
350 40 ▶ $self->{reported} = 1 if $fail;
Branch: 1 branch
BranchTrueFalseTotal
if $fail 11 29 40
351  
352 $is_future ? ( $done ? $code->done( $self->{result}->@* ) :
353 40 ▶ $fail ? $code->fail( $self->{failure}->@* ) :
Branch: 3 branches
BranchTrueFalseTotal
$is_future ? : 33 7 40
$done ? : 24 9 33
$fail ? : 8 1 9
354 $code->cancel )
355 : $code->( $self );
356 }
357 else {
358 139 push $self->{callbacks}->@*, [ CB_ALWAYS|CB_SELF, $self->wrap_cb( on_ready => $code ) ];
359 }
360  
361 179 return $self;
362 }
363  
364 # An optimised version for Awaitable role
365 0 ▶sub AWAIT_ON_READY ( $self, $code )
Subroutine AWAIT_ON_READY: not called CC=1 SCAR=6.9
Pod undocumented
366 {
367 0 push $self->{callbacks}->@*, [ CB_ALWAYS|CB_SELF, $self->wrap_cb( on_ready => $code ) ];
368 }
369  
370 91 ▶sub result ( $self )
Subroutine result: called CC=8 SCAR=20.8
Pod undocumented
371 {
372 $self->{ready} or
373 91 ▶ Carp::croak( "${\$self->__selfstr} is not yet ready" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ready"} or Carp::croak("${\$self->__selfstr;} is not yet ready") 90 1 91
374 90 ▶ if( my $failure = $self->{failure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $failure = $self->{"failure"}) 9 81 90
375 9 $self->{reported} = 1;
376 9 my $exception = $failure->[0];
377 9 ▶ $exception = Future::Exception->new( @$failure ) if @$failure > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$failure > 1 2 7 9
378 9 ▶ !ref $exception && $exception =~ m/\n$/ ? CORE::die $exception : Carp::croak $exception;
Branch: 1 branch
BranchTrueFalseTotal
!ref($exception) && $exception =~ /\n$/u ? : 3 6 9
Condition100%3/3
not ref $exception and $exception =~ /\n$/u
!ll&&!rl&&r
243
MC/DC100%2/2
not ref $exception and $exception =~ /\n$/u
not ref $exception $exception =~ /\n$/u
Truth table100%3/3
not(A) and B
A= not ref $exceptionB= $exception =~ /\n$/u
ABresult
0X0
100
111
379 }
380 81 ▶ $self->{cancelled} and Carp::croak "${\$self->__selfstr} was cancelled";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cancelled"} and Carp::croak("${\$self->__selfstr;} was cancelled") 2 79 81
381 79 ▶ return $self->{result}->[0] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 41 38 79
382 38 return $self->{result}->@*;
383 }
384  
385 10 ▶sub get ( $self )
Subroutine get: called CC=2 SCAR=6.9
Pod undocumented
386 {
387 10 ▶ $self->await unless $self->{ready};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ready"} 5 5 10
388 9 return $self->result;
389 }
390  
391 4 ▶sub await ( $self )
Subroutine await: called CC=2 SCAR=6.9
Pod undocumented
392 {
393 4 ▶ return $self if $self->{ready};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ready"} 3 1 4
394 1 Carp::croak "$self is not yet complete and does not provide ->await";
395 }
396  
397 90 ▶sub on_done ( $self, $code )
Subroutine on_done: called CC=8 SCAR=20.9
Pod undocumented
398 {
399 90 ▶ my $is_future = blessed( $code ) && $code->isa( "Future" );
Condition66%2/3
builtin::blessed($code) && $code->isa('Future')
!ll&&!rl&&r
64026
MC/DC50%1/2
builtin::blessed($code) && $code->isa('Future')
builtin::blessed($code) $code->isa('Future')
Truth table66%2/3
A && B
A= builtin::blessed($code)B= $code->isa('Future')
ABresult
0X0
100
111
400 90 ▶ $is_future or _callable( $code ) or
Branch: 1 branch
BranchTrueFalseTotal
$is_future or _callable($code) or Carp::croak("Expected \$code to be callable or a Future in ->on_done") 90 0 90
Condition66%2/3
$is_future or _callable($code)
l!l&&r!l&&!r
26640
MC/DC0%0/2
$is_future or _callable($code)
$is_future _callable($code)
Truth table66%2/3
A or B
A= $is_futureB= _callable($code)
ABresult
1X1
011
000
401 Carp::croak "Expected \$code to be callable or a Future in ->on_done";
402  
403 90 ▶ if( $self->{ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ready'}) { } 27 63 90
404 27 ▶ return $self if $self->{failure} or $self->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failure"} or $self->{"cancelled"} 12 15 27
Condition100%3/3
$self->{"failure"} or $self->{"cancelled"}
l!l&&r!l&&!r
10215
MC/DC100%2/2
$self->{"failure"} or $self->{"cancelled"}
$self->{"failure"} $self->{"cancelled"}
Truth table100%3/3
A or B
A= $self->{"failure"}B= $self->{"cancelled"}
ABresult
1X1
011
000
405  
406 $is_future ? $code->done( $self->{result}->@* )
407 15 ▶ : $code->( $self->{result}->@* );
Branch: 1 branch
BranchTrueFalseTotal
$is_future ? : 14 1 15
408 }
409 else {
410 63 push $self->{callbacks}->@*, [ CB_DONE|CB_RESULT, $self->wrap_cb( on_done => $code ) ];
411 }
412  
413 78 return $self;
414 }
415  
416 144 ▶sub failure ( $self )
Subroutine failure: called CC=4 SCAR=13.9
Pod undocumented
417 {
418 144 ▶ $self->await unless $self->{ready};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ready"} 2 142 144
419 144 ▶ return unless $self->{failure};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"failure"} 58 86 144
420 86 $self->{reported} = 1;
421 86 ▶ return $self->{failure}->[0] if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 69 17 86
422 17 return $self->{failure}->@*;
423 }
424  
425 88 ▶sub on_fail ( $self, $code )
Subroutine on_fail: called CC=7 SCAR=19.6
Pod undocumented
426 {
427 88 ▶ my $is_future = blessed( $code ) && $code->isa( "Future" );
Condition66%2/3
builtin::blessed($code) && $code->isa('Future')
!ll&&!rl&&r
26062
MC/DC50%1/2
builtin::blessed($code) && $code->isa('Future')
builtin::blessed($code) $code->isa('Future')
Truth table66%2/3
A && B
A= builtin::blessed($code)B= $code->isa('Future')
ABresult
0X0
100
111
428 88 ▶ $is_future or _callable( $code ) or
Branch: 1 branch
BranchTrueFalseTotal
$is_future or _callable($code) or Carp::croak("Expected \$code to be callable or a Future in ->on_fail") 88 0 88
Condition66%2/3
$is_future or _callable($code)
l!l&&r!l&&!r
62260
MC/DC0%0/2
$is_future or _callable($code)
$is_future _callable($code)
Truth table66%2/3
A or B
A= $is_futureB= _callable($code)
ABresult
1X1
011
000
429 Carp::croak "Expected \$code to be callable or a Future in ->on_fail";
430  
431 88 ▶ if( $self->{ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ready'}) { } 30 58 88
432 30 ▶ return $self if not $self->{failure};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"failure"} 18 12 30
433 12 $self->{reported} = 1;
434  
435 $is_future ? $code->fail( $self->{failure}->@* )
436 12 ▶ : $code->( $self->{failure}->@* );
Branch: 1 branch
BranchTrueFalseTotal
$is_future ? : 10 2 12
437 }
438 else {
439 58 push $self->{callbacks}->@*, [ CB_FAIL|CB_RESULT, $self->wrap_cb( on_fail => $code ) ];
440 }
441  
442 70 return $self;
443 }
444  
445 121 ▶sub cancel ( $self )
Subroutine cancel: called CC=7 SCAR=19.5
Pod undocumented
446 {
447 121 ▶ return $self if $self->{ready};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ready"} 8 113 121
448  
449 113 $self->{cancelled}++;
450 113 my $on_cancel = delete $self->{on_cancel};
451 113 ▶ foreach my $code ( $on_cancel ? reverse @$on_cancel : () ) {
Branch: 1 branch
BranchTrueFalseTotal
$on_cancel ? : 43 70 113
452 59 ▶ defined $code or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $code or next 46 13 59
453 46 ▶ my $is_future = blessed( $code ) && $code->isa( "Future" );
Condition66%2/3
builtin::blessed($code) && $code->isa('Future')
!ll&&!rl&&r
26020
MC/DC50%1/2
builtin::blessed($code) && $code->isa('Future')
builtin::blessed($code) $code->isa('Future')
Truth table66%2/3
A && B
A= builtin::blessed($code)B= $code->isa('Future')
ABresult
0X0
100
111
454 46 ▶ $is_future ? $code->cancel
Branch: 1 branch
BranchTrueFalseTotal
$is_future ? : 20 26 46
455 : $code->( $self );
456 }
457 113 $self->_mark_ready( "cancel" );
458  
459 113 return $self;
460 }
461  
462 21 ▶my $make_donecatchfail_sub = sub ( $with_f, $done_code, $fail_code, @catch_list ) {
Subroutine __ANON__: called CC=7 SCAR=19.6
463 21 my $func = (caller 1)[3];
464 21 $func =~ s/^.*:://;
465  
466 21 ▶ !$done_code or _callable( $done_code ) or
Branch: 1 branch
BranchTrueFalseTotal
not $done_code or _callable($done_code) or Carp::croak("Expected \$done_code to be callable in ->$func") 21 0 21
Condition66%2/3
not $done_code or _callable($done_code)
l!l&&r!l&&!r
8130
MC/DC0%0/2
not $done_code or _callable($done_code)
not $done_code _callable($done_code)
Truth table66%2/3
not(A) or B
A= not $done_codeB= _callable($done_code)
ABresult
1X1
011
000
467 Carp::croak "Expected \$done_code to be callable in ->$func";
468 21 ▶ !$fail_code or _callable( $fail_code ) or
Branch: 1 branch
BranchTrueFalseTotal
not $fail_code or _callable($fail_code) or Carp::croak("Expected \$fail_code to be callable in ->$func") 21 0 21
Condition66%2/3
not $fail_code or _callable($fail_code)
l!l&&r!l&&!r
8130
MC/DC0%0/2
not $fail_code or _callable($fail_code)
not $fail_code _callable($fail_code)
Truth table66%2/3
not(A) or B
A= not $fail_codeB= _callable($fail_code)
ABresult
1X1
011
000
469 Carp::croak "Expected \$fail_code to be callable in ->$func";
470  
471 21 my %catch_handlers = @catch_list;
472 _callable( $catch_handlers{$_} ) or
473 Carp::croak "Expected catch handler for '$_' to be callable in ->$func"
474 21 ▶ for keys %catch_handlers;
Branch: 1 branch
BranchTrueFalseTotal
_callable($catch_handlers{$_}) or Carp::croak("Expected catch handler for '${_}' to be callable in ->$func") 8 0 8
475  
476 19 ▶ sub ( $self ) {
Subroutine __ANON__: called CC=7 SCAR=19.5
477 19 ▶ my @maybe_self = $with_f ? ( $self ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$with_f ? : 7 12 19
478  
479 19 ▶ if( !$self->{failure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'failure'}) { } 10 9 19
480 10 ▶ return $self unless $done_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $done_code 3 7 10
481 7 return $done_code->( @maybe_self, $self->{result}->@* );
482 }
483 else {
484 9 my $name = $self->{failure}[1];
485 9 ▶ if( defined $name and $catch_handlers{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $catch_handlers{$name}) 4 5 9
Condition100%3/3
defined $name and $catch_handlers{$name}
!ll&&!rl&&r
324
MC/DC100%2/2
defined $name and $catch_handlers{$name}
defined $name $catch_handlers{$name}
Truth table100%3/3
A and B
A= defined $nameB= $catch_handlers{$name}
ABresult
0X0
100
111
486 4 return $catch_handlers{$name}->( @maybe_self, $self->{failure}->@* );
487 }
488 5 ▶ return $self unless $fail_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $fail_code 2 3 5
489 3 return $fail_code->( @maybe_self, $self->{failure}->@* );
490 }
491 21 };
492 33 };
493  
494 113 ▶sub _sequence ( $f1, $code, $flags )
Subroutine _sequence: called CC=21 SCAR=30.5
495 {
496 113 $flags |= CB_SEQ_STRICT if STRICT;
497  
498 # For later, we might want to know where we were called from
499 113 my $level = 1;
500 113 ▶ $level++ while (caller $level)[0] eq "Future::_base";
Branch: 1 branch
BranchTrueFalseTotal
(caller $level)[0] eq "Future::_base" and do { ++$level } 6 113 119
501 113 my $func = (caller $level)[3];
502 113 $func =~ s/^.*:://;
503  
504 113 ▶ $flags & (CB_SEQ_IMDONE|CB_SEQ_IMFAIL) or _callable( $code ) or
Branch: 1 branch
BranchTrueFalseTotal
$flags & 384 or _callable($code) or Carp::croak("Expected \$code to be callable in ->$func") 113 0 113
Condition66%2/3
$flags & 384 or _callable($code)
l!l&&r!l&&!r
121010
MC/DC0%0/2
$flags & 384 or _callable($code)
$flags & 384 _callable($code)
Truth table66%2/3
A or B
A= $flags & 384B= _callable($code)
ABresult
1X1
011
000
505 Carp::croak "Expected \$code to be callable in ->$func";
506  
507 113 ▶ if( !defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 4 109 113
508 4 Carp::carp "Calling ->$func in void context";
509 }
510  
511 113 ▶ if( $f1->is_ready ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f1->is_ready) 40 73 113
512 # Take a shortcut
513 return $f1 if $f1->is_done and not( $flags & CB_SEQ_ONDONE ) or
514 40 ▶ $f1->{failure} and not( $flags & CB_SEQ_ONFAIL );
Branch: 1 branch
BranchTrueFalseTotal
if $f1->is_done and not $flags & 32 or $f1->{"failure"} and not $flags & 64 3 37 40
Condition100%9/9
$f1->is_done and not $flags & 32 or $f1->{"failure"} and not $flags & 64
l!l&&r!l&&!r
2137
$f1->is_done and not $flags & 32
!ll&&!rl&&r
10282
$f1->{"failure"} and not $flags & 64
!ll&&!rl&&r
2891
MC/DC75%3/4
$f1->is_done and not $flags & 32 or $f1->{"failure"} and not $flags & 64
$f1->is_done not $flags & 32 $f1->{"failure"} not $flags & 64
Truth table57%4/7
A and not(B) or C and not(D)
A= $f1->is_doneB= not $flags & 32C= $f1->{"failure"}D= not $flags & 64
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
515  
516 37 ▶ if( $flags & CB_SEQ_IMDONE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($flags & 128) { } 2 35 37
elsif ($flags & 256) { } 2 33 35
517 2 return Future->done( @$code );
518 }
519 elsif( $flags & CB_SEQ_IMFAIL ) {
520 2 return Future->fail( @$code );
521 }
522  
523 my @args = (
524 ( $flags & CB_SELF ? $f1 : () ),
525 ( $flags & CB_RESULT ? $f1->is_done ? $f1->{result}->@* :
526 $f1->{failure} ? $f1->{failure}->@* :
527 33 ▶ () : () ),
Branch: 4 branches
BranchTrueFalseTotal
$flags & 8 ? : 15 18 33
$flags & 16 ? : 20 13 33
$f1->is_done ? : 16 4 20
$f1->{'failure'} ? : 4 0 4
528 );
529  
530 33 my $fseq;
531 33 ▶ unless( eval { $fseq = $code->( @args ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $fseq = &$code(@args); 1 } }) 2 31 33
532 2 return Future->fail( $@ );
533 }
534  
535 31 ▶ unless( blessed $fseq and $fseq->isa( "Future" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($fseq) and $fseq->isa("Future")) 8 23 31
Condition66%2/3
builtin::blessed($fseq) and $fseq->isa("Future")
!ll&&!rl&&r
8023
MC/DC50%1/2
builtin::blessed($fseq) and $fseq->isa("Future")
builtin::blessed($fseq) $fseq->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($fseq)B= $fseq->isa("Future")
ABresult
0X0
100
111
536 # Upgrade a non-Future result, or complain in strict mode
537 8 ▶ $flags & CB_SEQ_STRICT and
Branch: 1 branch
BranchTrueFalseTotal
$flags & 512 and return "Future"->fail('Expected ' . Future::CvNAME_FILE_LINE($code) . ' to return a Future') 0 8 8
538 return Future->fail( "Expected " . Future::CvNAME_FILE_LINE($code) . " to return a Future" );
539  
540 8 $fseq = $f1->new->done( $fseq );
541 }
542  
543 31 return $fseq;
544 }
545  
546 73 my $fseq = $f1->new;
547 73 $fseq->on_cancel( $f1 );
548  
549 # TODO: if anyone cares about the op name, we might have to synthesize it
550 # from $flags
551 73 ▶ $code = $f1->wrap_cb( sequence => $code ) unless $flags & (CB_SEQ_IMDONE|CB_SEQ_IMFAIL);
Branch: 1 branch
BranchTrueFalseTotal
unless $flags & 384 65 8 73
552  
553 73 push $f1->{callbacks}->@*, [ CB_DONE|CB_FAIL|$flags, $code, $fseq ];
554 73 weaken( $f1->{callbacks}[-1][2] );
555  
556 73 return $fseq;
557 }
558  
559 50 ▶sub then ( $self, $done_code = undef, @catch_list )
Subroutine then: called CC=6 SCAR=17.9
Pod undocumented
560 {
561 50 ▶ my $fail_code = ( @catch_list % 2 ) ? pop @catch_list : undef;
Branch: 1 branch
BranchTrueFalseTotal
@catch_list % 2 ? : 5 45 50
562  
563 50 ▶ if( $done_code and !@catch_list and !$fail_code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_code and not @catch_list and not $fail_code) 43 7 50
Condition100%6/6
$done_code and not @catch_list and not $fail_code
!ll&&!rl&&r
3443
$done_code and not @catch_list
!ll&&!rl&&r
2147
MC/DC100%3/3
$done_code and not @catch_list and not $fail_code
$done_code not @catch_list not $fail_code
Truth table100%4/4
A and not(B) and not(C)
A= $done_codeB= not @catch_listC= not $fail_code
ABCresult
0XX0
10X0
1100
1111
564 43 return $self->_sequence( $done_code, CB_SEQ_ONDONE|CB_RESULT );
565 }
566  
567 # Complex
568 7 return $self->_sequence( $make_donecatchfail_sub->(
569 0, $done_code, $fail_code, @catch_list,
570 ), CB_SEQ_ONDONE|CB_SEQ_ONFAIL|CB_SELF );
571 }
572  
573 3 ▶sub then_done ( $self, @result )
Subroutine then_done: called CC=1 SCAR=0.0
Pod undocumented
574 {
575 3 return $self->_sequence( \@result, CB_SEQ_ONDONE|CB_SEQ_IMDONE );
576 }
577  
578 3 ▶sub then_fail ( $self, @failure )
Subroutine then_fail: called CC=1 SCAR=0.0
Pod undocumented
579 {
580 3 return $self->_sequence( \@failure, CB_SEQ_ONDONE|CB_SEQ_IMFAIL );
581 }
582  
583 15 ▶sub else ( $self, $fail_code )
Subroutine else: called CC=1 SCAR=0.0
Pod undocumented
584 {
585 15 return $self->_sequence( $fail_code, CB_SEQ_ONFAIL|CB_RESULT );
586 }
587  
588 3 ▶sub else_done ( $self, @result )
Subroutine else_done: called CC=1 SCAR=0.0
Pod undocumented
589 {
590 3 return $self->_sequence( \@result, CB_SEQ_ONFAIL|CB_SEQ_IMDONE );
591 }
592  
593 3 ▶sub else_fail ( $self, @failure )
Subroutine else_fail: called CC=1 SCAR=0.0
Pod undocumented
594 {
595 3 return $self->_sequence( \@failure, CB_SEQ_ONFAIL|CB_SEQ_IMFAIL );
596 }
597  
598 5 ▶sub catch ( $self, @catch_list )
Subroutine catch: called CC=2 SCAR=6.9
Pod undocumented
599 {
600 5 ▶ my $fail_code = ( @catch_list % 2 ) ? pop @catch_list : undef;
Branch: 1 branch
BranchTrueFalseTotal
@catch_list % 2 ? : 1 4 5
601  
602 5 return $self->_sequence( $make_donecatchfail_sub->(
603 0, undef, $fail_code, @catch_list,
604 ), CB_SEQ_ONDONE|CB_SEQ_ONFAIL|CB_SELF );
605 }
606  
607 11 ▶sub then_with_f ( $self, $done_code, @catch_list )
Subroutine then_with_f: called CC=5 SCAR=16.1
Pod undocumented
608 {
609 11 ▶ my $fail_code = ( @catch_list % 2 ) ? pop @catch_list : undef;
Branch: 1 branch
BranchTrueFalseTotal
@catch_list % 2 ? : 7 4 11
610  
611 11 ▶ if( $done_code and !@catch_list and !$fail_code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_code and not @catch_list and not $fail_code) 3 8 11
Condition83%5/6
$done_code and not @catch_list and not $fail_code
!ll&&!rl&&r
173
$done_code and not @catch_list
!ll&&!rl&&r
0110
MC/DC66%2/3
$done_code and not @catch_list and not $fail_code
$done_code not @catch_list not $fail_code
Truth table75%3/4
A and not(B) and not(C)
A= $done_codeB= not @catch_listC= not $fail_code
ABCresult
0XX0
10X0
1100
1111
612 3 return $self->_sequence( $done_code, CB_SEQ_ONDONE|CB_SELF|CB_RESULT );
613 }
614  
615 8 return $self->_sequence( $make_donecatchfail_sub->(
616 1, $done_code, $fail_code, @catch_list,
617 ), CB_SEQ_ONDONE|CB_SEQ_ONFAIL|CB_SELF );
618 }
619  
620 3 ▶sub else_with_f ( $self, $fail_code )
Subroutine else_with_f: called CC=1 SCAR=0.0
Pod undocumented
621 {
622 3 return $self->_sequence( $fail_code, CB_SEQ_ONFAIL|CB_SELF|CB_RESULT );
623 }
624  
625 1 ▶sub catch_with_f ( $self, @catch_list )
Subroutine catch_with_f: called CC=2 SCAR=7.2
Pod undocumented
626 {
627 1 ▶ my $fail_code = ( @catch_list % 2 ) ? pop @catch_list : undef;
Branch: 1 branch
BranchTrueFalseTotal
@catch_list % 2 ? : 0 1 1
628  
629 1 return $self->_sequence( $make_donecatchfail_sub->(
630 1, undef, $fail_code, @catch_list,
631 ), CB_SEQ_ONDONE|CB_SEQ_ONFAIL|CB_SELF );
632 }
633  
634 16 ▶sub followed_by ( $self, $code )
Subroutine followed_by: called CC=1 SCAR=0.0
Pod undocumented
635 {
636 16 return $self->_sequence( $code, CB_SEQ_ONDONE|CB_SEQ_ONFAIL|CB_SELF );
637 }
638  
639 4 ▶sub without_cancel ( $self )
Subroutine without_cancel: called CC=1 SCAR=0.0
Pod undocumented
640 {
641 4 my $new = $self->new;
642  
643 4 ▶ $self->on_ready( sub ( $self ) {
Subroutine __ANON__: called CC=3 SCAR=11.4
644 4 ▶ if( $self->{cancelled} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'cancelled'}) { } 1 3 4
elsif ($self->{'failure'}) { } 0 3 3
645 1 $new->cancel;
646 }
647 elsif( $self->{failure} ) {
648 0 $new->fail( $self->{failure}->@* );
649 }
650 else {
651 3 $new->done( $self->{result}->@* );
652 }
653 4 });
654  
655 4 $new->{orig} = $self; # just to strongref it - RT122920
656 4 ▶ $new->on_ready( sub ( $self ) { undef $self->{orig} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
657  
658 4 return $new;
659 }
660  
661 # $self->{subs} is an even-sized list of *pairs*, ( $subf, $flags, $subf, $flags, ... )
662 # pairkeys @{ $self->{subs} } yields just the futures
663  
664 33 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
665 33 SUBFLAG_NO_CANCEL => (1<<0),
666 33 };
667  
668 57 ▶sub _new_convergent ( $, $subs )
Subroutine _new_convergent: called CC=9 SCAR=22.3
669 {
670 57 my @flaggedsubs;
671  
672 57 ▶ for ( my $i = 0; $i < @$subs; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$subs) 101 57 158
673 101 my $flags = 0;
674 101 ▶ $flags |= SUBFLAG_NO_CANCEL, $i++ if !blessed $subs->[$i] and $subs->[$i] eq "also";
Branch: 1 branch
BranchTrueFalseTotal
if not builtin::blessed($subs->[$i]) and $subs->[$i] eq "also" 8 93 101
Condition66%2/3
not builtin::blessed($subs->[$i]) and $subs->[$i] eq "also"
!ll&&!rl&&r
9308
MC/DC50%1/2
not builtin::blessed($subs->[$i]) and $subs->[$i] eq "also"
not builtin::blessed($subs->[$i]) $subs->[$i] eq "also"
Truth table66%2/3
not(A) and B
A= not builtin::blessed($subs->[$i])B= $subs->[$i] eq "also"
ABresult
0X0
100
111
675  
676 101 my $sub = $subs->[$i];
677 101 ▶ blessed $sub and $sub->isa( "Future" ) or Carp::croak "Expected a Future, got $sub";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($sub) and $sub->isa("Future") or Carp::croak("Expected a Future, got $sub") 101 0 101
Condition33%1/3
builtin::blessed($sub) and $sub->isa("Future")
!ll&&!rl&&r
00101
MC/DC0%0/2
builtin::blessed($sub) and $sub->isa("Future")
builtin::blessed($sub) $sub->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($sub)B= $sub->isa("Future")
ABresult
0X0
100
111
678  
679 101 push @flaggedsubs, ( $sub, $flags );
680 }
681  
682 # Find the best prototype. Ideally anything derived if we can find one.
683 57 my $self;
684 57 ▶ ref($_) eq "Future" or $self = $_->new, last for pairkeys @flaggedsubs;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "Future" or $self = $_->new, last 92 7 99
685  
686 # No derived ones; just have to be a basic class then
687 57 ▶ $self ||= Future->new;
Condition66%2/3
$self ||= "Future"->new
l!l&&r!l&&!r
7500
MC/DC0%0/2
$self ||= "Future"->new
$self "Future"->new
Truth table66%2/3
A ||= B
A= $selfB= "Future"->new
ABresult
1X1
011
000
688  
689 57 $self->{subs} = \@flaggedsubs;
690  
691 57 $self->on_cancel( \&_cancel_subs );
692  
693 57 @$subs = pairkeys @flaggedsubs;
694  
695 57 return $self;
696 }
697  
698 # This might be called by a DESTROY during global destruction so it should
699 # be as defensive as possible (see RT88967)
700 41 ▶sub _cancel_subs ( $self )
Subroutine _cancel_subs: called CC=6 SCAR=18.1
701 {
702 41 ▶ my $subs = $self->{subs} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $subs = $self->{"subs"} or return 41 0 41
703  
704 41 foreach ( pairs @$subs ) {
705 72 my ( $sub, $flags ) = @$_;
706 72 ▶ $sub->cancel if !( $flags & SUBFLAG_NO_CANCEL ) and $sub and !$sub->{ready};
Branch: 1 branch
BranchTrueFalseTotal
if not $flags & 1 and $sub and not $sub->{"ready"} 19 53 72
Condition83%5/6
not $flags & 1 and $sub and not $sub->{"ready"}
!ll&&!rl&&r
84519
not $flags & 1 and $sub
!ll&&!rl&&r
8064
MC/DC66%2/3
not $flags & 1 and $sub and not $sub->{"ready"}
not $flags & 1 $sub not $sub->{"ready"}
Truth table75%3/4
not(A) and B and not(C)
A= not $flags & 1B= $subC= not $sub->{"ready"}
ABCresult
0XX0
10X0
1100
1111
707 }
708 }
709  
710 14 ▶sub wait_all ( $class, @subs )
Subroutine wait_all: called CC=7 SCAR=19.5
Pod undocumented
711 {
712 14 ▶ unless( @subs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subs) 2 12 14
713 2 my $self = $class->done;
714 2 $self->{subs} = [];
715 2 return $self;
716 }
717  
718 12 my $self = Future->_new_convergent( \@subs );
719  
720 12 my $pending = 0;
721 12 ▶ $_->{ready} or $pending++ for @subs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"ready"} or ++$pending 4 16 20
722  
723 # Look for immediate ready
724 12 ▶ if( !$pending ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pending) 1 11 12
725 1 $self->{result} = [ @subs ];
726 1 $self->_mark_ready( "wait_all" );
727 1 return $self;
728 }
729  
730 11 weaken( my $weakself = $self );
731 15 ▶ my $sub_on_ready = sub ( $f ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
732 15 ▶ return unless my $self = $weakself;
Branch: 1 branch
BranchTrueFalseTotal
unless my $self = $weakself 0 15 15
733  
734 15 $pending--;
735 15 ▶ $pending and return;
Branch: 1 branch
BranchTrueFalseTotal
$pending and return 5 10 15
736  
737 10 $self->{result} = [ pairkeys $self->{subs}->@* ];
738 10 $self->_mark_ready( "wait_all" );
739 11 };
740  
741 11 foreach my $sub ( @subs ) {
742 19 ▶ $sub->{ready} or $sub->on_ready( $sub_on_ready );
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"ready"} or $sub->on_ready($sub_on_ready) 3 16 19
743 }
744  
745 11 return $self;
746 }
747  
748 18 ▶sub wait_any ( $class, @subs )
Subroutine wait_any: called CC=9 SCAR=22.0
Pod undocumented
749 {
750 18 ▶ unless( @subs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subs) 2 16 18
751 2 my $self = $class->fail( "Cannot ->wait_any with no subfutures" );
752 2 $self->{subs} = [];
753 2 return $self;
754 }
755  
756 16 my $self = Future->_new_convergent( \@subs );
757  
758 # Look for immediate ready
759 16 my $immediate_ready;
760 16 foreach my $sub ( @subs ) {
761 27 ▶ $sub->{ready} and !$sub->{cancelled} and $immediate_ready = $sub, last;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"ready"} and not $sub->{"cancelled"} and $immediate_ready = $sub, last 2 25 27
Condition100%3/3
$sub->{"ready"} and not $sub->{"cancelled"}
!ll&&!rl&&r
2412
MC/DC100%2/2
$sub->{"ready"} and not $sub->{"cancelled"}
$sub->{"ready"} not $sub->{"cancelled"}
Truth table100%3/3
A and not(B)
A= $sub->{"ready"}B= not $sub->{"cancelled"}
ABresult
0X0
100
111
762 }
763  
764 16 ▶ if( $immediate_ready ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($immediate_ready) 2 14 16
765 2 $self->_cancel_subs;
766  
767 2 ▶ if( $immediate_ready->{failure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($immediate_ready->{'failure'}) { } 0 2 2
768 0 $self->{failure} = [ $immediate_ready->{failure}->@* ];
769 }
770 else {
771 2 $self->{result} = [ $immediate_ready->{result}->@* ];
772 }
773 2 $self->_mark_ready( "wait_any" );
774 2 return $self;
775 }
776  
777 14 my $pending = 0;
778  
779 14 weaken( my $weakself = $self );
780 21 ▶ my $sub_on_ready = sub ( $f ) {
Subroutine __ANON__: called CC=8 SCAR=20.8
781 21 ▶ return unless my $self = $weakself;
Branch: 1 branch
BranchTrueFalseTotal
unless my $self = $weakself 0 21 21
782 21 ▶ return if $self->{result} or $self->{failure}; # don't recurse on child ->cancel
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"result"} or $self->{"failure"} 6 15 21
Condition100%3/3
$self->{"result"} or $self->{"failure"}
l!l&&r!l&&!r
4215
MC/DC100%2/2
$self->{"result"} or $self->{"failure"}
$self->{"result"} $self->{"failure"}
Truth table100%3/3
A or B
A= $self->{"result"}B= $self->{"failure"}
ABresult
1X1
011
000
783  
784 15 ▶ return if --$pending and $f->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if --$pending and $f->{"cancelled"} 2 13 15
Condition100%3/3
--$pending and $f->{"cancelled"}
!ll&&!rl&&r
582
MC/DC100%2/2
--$pending and $f->{"cancelled"}
--$pending $f->{"cancelled"}
Truth table100%3/3
A and B
A= --$pendingB= $f->{"cancelled"}
ABresult
0X0
100
111
785  
786 13 ▶ if( $f->{cancelled} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f->{'cancelled'}) { } 2 11 13
elsif ($f->{'failure'}) { } 3 8 11
787 2 $self->{failure} = [ "All component futures were cancelled" ];
788 }
789 elsif( $f->{failure} ) {
790 3 $self->{failure} = [ $f->{failure}->@* ];
791 }
792 else {
793 8 $self->{result} = [ $f->{result}->@* ];
794 }
795  
796 13 $self->_cancel_subs;
797  
798 13 $self->_mark_ready( "wait_any" );
799 14 };
800  
801 14 foreach my $sub ( @subs ) {
802 # No need to test $sub->{ready} since we know none of them are
803 25 ▶ next if $sub->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if $sub->{"cancelled"} 1 24 25
804 24 $sub->on_ready( $sub_on_ready );
805 24 $pending++;
806 }
807  
808 14 return $self;
809 }
810  
811 18 ▶sub needs_all ( $class, @subs )
Subroutine needs_all: called CC=12 SCAR=24.9
Pod undocumented
812 {
813 18 ▶ unless( @subs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subs) 2 16 18
814 2 my $self = $class->done;
815 2 $self->{subs} = [];
816 2 return $self;
817 }
818  
819 16 my $self = Future->_new_convergent( \@subs );
820  
821 # Look for immediate fail
822 16 my $immediate_failure;
823 16 foreach my $sub ( @subs ) {
824 30 ▶ $sub->{cancelled} and $immediate_failure = [ "A component future was cancelled" ], last;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"cancelled"} and $immediate_failure = ["A component future was cancelled"], last 1 29 30
825 29 ▶ $sub->{ready} and $sub->{failure} and $immediate_failure = $sub->{failure}, last;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"ready"} and $sub->{"failure"} and $immediate_failure = $sub->{"failure"}, last 1 28 29
Condition100%3/3
$sub->{"ready"} and $sub->{"failure"}
!ll&&!rl&&r
2711
MC/DC100%2/2
$sub->{"ready"} and $sub->{"failure"}
$sub->{"ready"} $sub->{"failure"}
Truth table100%3/3
A and B
A= $sub->{"ready"}B= $sub->{"failure"}
ABresult
0X0
100
111
826 }
827  
828 16 ▶ if( $immediate_failure ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($immediate_failure) 2 14 16
829 2 $self->_cancel_subs;
830  
831 2 $self->{failure} = [ @$immediate_failure ];
832 2 $self->_mark_ready( "needs_all" );
833 2 return $self;
834 }
835  
836 14 my $pending = 0;
837 14 ▶ $_->{ready} or $pending++ for @subs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"ready"} or ++$pending 1 26 27
838  
839 # Look for immediate done
840 14 ▶ if( !$pending ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pending) 1 13 14
841 1 $self->{result} = [ map { $_->{result}->@* } @subs ];
842 1 $self->_mark_ready( "needs_all" );
843 1 return $self;
844 }
845  
846 13 weaken( my $weakself = $self );
847 24 ▶ my $sub_on_ready = sub ( $f ) {
Subroutine __ANON__: called CC=7 SCAR=19.5
848 24 ▶ return unless my $self = $weakself;
Branch: 1 branch
BranchTrueFalseTotal
unless my $self = $weakself 0 24 24
849 24 ▶ return if $self->{result} or $self->{failure}; # don't recurse on child ->cancel
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"result"} or $self->{"failure"} 5 19 24
Condition66%2/3
$self->{"result"} or $self->{"failure"}
l!l&&r!l&&!r
0519
MC/DC50%1/2
$self->{"result"} or $self->{"failure"}
$self->{"result"} $self->{"failure"}
Truth table66%2/3
A or B
A= $self->{"result"}B= $self->{"failure"}
ABresult
1X1
011
000
850  
851 19 ▶ if( $f->{cancelled} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f->{'cancelled'}) { } 4 15 19
elsif ($f->{'failure'}) { } 4 11 15
852 4 $self->{failure} = [ "A component future was cancelled" ];
853 4 $self->_cancel_subs;
854 4 $self->_mark_ready( "needs_all" );
855 }
856 elsif( $f->{failure} ) {
857 4 $self->{failure} = [ $f->{failure}->@* ];
858 4 $self->_cancel_subs;
859 4 $self->_mark_ready( "needs_all" );
860 }
861 else {
862 11 $pending--;
863 11 ▶ $pending and return;
Branch: 1 branch
BranchTrueFalseTotal
$pending and return 7 4 11
864  
865 4 $self->{result} = [ map { $_->{result}->@* } pairkeys $self->{subs}->@* ];
866 4 $self->_mark_ready( "needs_all" );
867 }
868 13 };
869  
870 13 foreach my $sub ( @subs ) {
871 26 ▶ $sub->{ready} or $sub->on_ready( $sub_on_ready );
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"ready"} or $sub->on_ready($sub_on_ready) 0 26 26
872 }
873  
874 13 return $self;
875 }
876  
877 15 ▶sub needs_any ( $class, @subs )
Subroutine needs_any: called CC=16 SCAR=27.7
Pod undocumented
878 {
879 15 ▶ unless( @subs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subs) 2 13 15
880 2 my $self = $class->fail( "Cannot ->needs_any with no subfutures" );
881 2 $self->{subs} = [];
882 2 return $self;
883 }
884  
885 13 my $self = Future->_new_convergent( \@subs );
886  
887 # Look for immediate done
888 13 my $immediate_done;
889 13 my $pending = 0;
890 13 foreach my $sub ( @subs ) {
891 22 ▶ $sub->{ready} and !$sub->{failure} and !$sub->{cancelled} and $immediate_done = $sub, last;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"ready"} and not $sub->{"failure"} and not $sub->{"cancelled"} and $immediate_done = $sub, last 1 21 22
Condition100%6/6
$sub->{"ready"} and not $sub->{"failure"} and not $sub->{"cancelled"}
!ll&&!rl&&r
2011
$sub->{"ready"} and not $sub->{"failure"}
!ll&&!rl&&r
1822
MC/DC100%3/3
$sub->{"ready"} and not $sub->{"failure"} and not $sub->{"cancelled"}
$sub->{"ready"} not $sub->{"failure"} not $sub->{"cancelled"}
Truth table100%4/4
A and not(B) and not(C)
A= $sub->{"ready"}B= not $sub->{"failure"}C= not $sub->{"cancelled"}
ABCresult
0XX0
10X0
1100
1111
892 21 ▶ $sub->{ready} or $pending++;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"ready"} or ++$pending 3 18 21
893 }
894  
895 13 ▶ if( $immediate_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($immediate_done) 1 12 13
896 1 foreach my $sub ( @subs ) {
897 2 ▶ $sub->{ready} ? $sub->{reported} = 1 : $sub->cancel;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{'ready'} ? : 2 0 2
898 }
899  
900 1 $self->{result} = [ $immediate_done->{result}->@* ];
901 1 $self->_mark_ready( "needs_any" );
902 1 return $self;
903 }
904  
905 # Look for immediate fail
906 12 my $immediate_fail = 1;
907 12 foreach my $sub ( @subs ) {
908 12 ▶ $sub->{ready} or $immediate_fail = 0, last;
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"ready"} or $immediate_fail = 0, last 1 11 12
909 }
910  
911 12 ▶ if( $immediate_fail ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($immediate_fail) 1 11 12
912 1 $_->{reported} = 1 for @subs;
913 # For consistency we'll pick the last one for the failure
914 1 $self->{failure} = [ $subs[-1]->{failure}->@* ];
915 1 $self->_mark_ready( "needs_any" );
916 1 return $self;
917 }
918  
919 11 weaken( my $weakself = $self );
920 16 ▶ my $sub_on_ready = sub ( $f ) {
Subroutine __ANON__: called CC=9 SCAR=22.0
921 16 ▶ return unless my $self = $weakself;
Branch: 1 branch
BranchTrueFalseTotal
unless my $self = $weakself 0 16 16
922 16 ▶ return if $self->{result} or $self->{failure}; # don't recurse on child ->cancel
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"result"} or $self->{"failure"} 2 14 16
Condition66%2/3
$self->{"result"} or $self->{"failure"}
l!l&&r!l&&!r
2014
MC/DC50%1/2
$self->{"result"} or $self->{"failure"}
$self->{"result"} $self->{"failure"}
Truth table66%2/3
A or B
A= $self->{"result"}B= $self->{"failure"}
ABresult
1X1
011
000
923  
924 14 ▶ return if --$pending and $f->{cancelled};
Branch: 1 branch
BranchTrueFalseTotal
if --$pending and $f->{"cancelled"} 2 12 14
Condition100%3/3
--$pending and $f->{"cancelled"}
!ll&&!rl&&r
752
MC/DC100%2/2
--$pending and $f->{"cancelled"}
--$pending $f->{"cancelled"}
Truth table100%3/3
A and B
A= --$pendingB= $f->{"cancelled"}
ABresult
0X0
100
111
925  
926 12 ▶ if( $f->{cancelled} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f->{'cancelled'}) { } 2 10 12
elsif ($f->{'failure'}) { } 3 7 10
927 2 $self->{failure} = [ "All component futures were cancelled" ];
928 2 $self->_mark_ready( "needs_any" );
929 }
930 elsif( $f->{failure} ) {
931 3 ▶ $pending and return;
Branch: 1 branch
BranchTrueFalseTotal
$pending and return 2 1 3
932  
933 1 $self->{failure} = [ $f->{failure}->@* ];
934 1 $self->_mark_ready( "needs_any" );
935 }
936 else {
937 7 $self->{result} = [ $f->{result}->@* ];
938 7 $self->_cancel_subs;
939 7 $self->_mark_ready( "needs_any" );
940 }
941 11 };
942  
943 11 foreach my $sub ( @subs ) {
944 19 ▶ $sub->{ready} or $sub->on_ready( $sub_on_ready );
Branch: 1 branch
BranchTrueFalseTotal
$sub->{"ready"} or $sub->on_ready($sub_on_ready) 1 18 19
945 }
946  
947 11 return $self;
948 }
949  
950 7 ▶sub pending_futures ( $self )
Subroutine pending_futures: called CC=2 SCAR=7.1
Pod undocumented
951 {
952 7 ▶ $self->{subs} or Carp::croak "Cannot call ->pending_futures on a non-convergent Future";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"subs"} or Carp::croak("Cannot call ->pending_futures on a non-convergent Future") 7 0 7
953 7 return grep { not $_->{ready} } pairkeys $self->{subs}->@*;
954 }
955  
956 7 ▶sub ready_futures ( $self )
Subroutine ready_futures: called CC=2 SCAR=7.1
Pod undocumented
957 {
958 7 ▶ $self->{subs} or Carp::croak "Cannot call ->ready_futures on a non-convergent Future";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"subs"} or Carp::croak("Cannot call ->ready_futures on a non-convergent Future") 7 0 7
959 7 return grep { $_->{ready} } pairkeys $self->{subs}->@*;
960 }
961  
962 9 ▶sub done_futures ( $self )
Subroutine done_futures: called CC=4 SCAR=13.9
Pod undocumented
963 {
964 9 ▶ $self->{subs} or Carp::croak "Cannot call ->done_futures on a non-convergent Future";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"subs"} or Carp::croak("Cannot call ->done_futures on a non-convergent Future") 9 0 9
965 9 ▶ return grep { $_->{ready} and not $_->{failure} and not $_->{cancelled} } pairkeys $self->{subs}->@*;
Condition100%6/6
$_->{'ready'} && !$_->{'failure'} && !$_->{'cancelled'}
!ll&&!rl&&r
369
$_->{"ready"} and not $_->{"failure"}
!ll&&!rl&&r
1215
MC/DC100%3/3
$_->{'ready'} && !$_->{'failure'} && !$_->{'cancelled'}
$_->{"ready"} not $_->{"failure"} !$_->{'cancelled'}
Truth table100%4/4
A and not(B) && not(C)
A= $_->{"ready"}B= not $_->{"failure"}C= !$_->{'cancelled'}
ABCresult
0XX0
10X0
1100
1111
966 }
967  
968 4 ▶sub failed_futures ( $self )
Subroutine failed_futures: called CC=3 SCAR=11.4
Pod undocumented
969 {
970 4 ▶ $self->{subs} or Carp::croak "Cannot call ->failed_futures on a non-convergent Future";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"subs"} or Carp::croak("Cannot call ->failed_futures on a non-convergent Future") 4 0 4
971 4 ▶ return grep { $_->{ready} and $_->{failure} } pairkeys $self->{subs}->@*;
Condition66%2/3
$_->{'ready'} && $_->{'failure'}
!ll&&!rl&&r
044
MC/DC50%1/2
$_->{'ready'} && $_->{'failure'}
$_->{'ready'} $_->{'failure'}
Truth table66%2/3
A && B
A= $_->{'ready'}B= $_->{'failure'}
ABresult
0X0
100
111
972 }
973  
974 6 ▶sub cancelled_futures ( $self )
Subroutine cancelled_futures: called CC=3 SCAR=11.4
Pod undocumented
975 {
976 6 ▶ $self->{subs} or Carp::croak "Cannot call ->cancelled_futures on a non-convergent Future";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"subs"} or Carp::croak("Cannot call ->cancelled_futures on a non-convergent Future") 6 0 6
977 6 ▶ return grep { $_->{ready} and $_->{cancelled} } pairkeys $self->{subs}->@*;
Condition66%2/3
$_->{'ready'} && $_->{'cancelled'}
!ll&&!rl&&r
066
MC/DC50%1/2
$_->{'ready'} && $_->{'cancelled'}
$_->{'ready'} $_->{'cancelled'}
Truth table66%2/3
A && B
A= $_->{'ready'}B= $_->{'cancelled'}
ABresult
0X0
100
111
978 }
979  
980 11 ▶sub btime ( $self )
Subroutine btime: called CC=1 SCAR=0.0
Pod undocumented
981 {
982 11 return $self->{btime};
983 }
984  
985 12 ▶sub rtime ( $self )
Subroutine rtime: called CC=1 SCAR=0.0
Pod undocumented
986 {
987 12 return $self->{rtime};
988 }
989  
990 6 ▶sub set_label ( $self, $label )
Subroutine set_label: called CC=1 SCAR=0.0
Pod undocumented
991 {
992 6 $self->{label} = $label;
993 6 return $self;
994 }
995  
996 1 ▶sub label ( $self )
Subroutine label: called CC=1 SCAR=0.0
Pod undocumented
997 {
998 1 return $self->{label};
999 }
1000  
1001 1 ▶sub set_udata ( $self, $name, $value )
Subroutine set_udata: called CC=1 SCAR=0.0
Pod undocumented
1002 {
1003 1 $self->{"u_$name"} = $value;
1004 1 return $self;
1005 }
1006  
1007 1 ▶sub udata ( $self, $name )
Subroutine udata: called CC=1 SCAR=0.0
Pod undocumented
1008 {
1009 1 return $self->{"u_$name"};
1010 }
1011  
1012 33 0x55AA;
↑ Summary Future/Utils.pm »