CPS.pm

filter: Statementstmt 97.0% 132 / 136 Branchbran 83.3% 25 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.8% 37 / 39 Podpod 36.3% 4 / 11 totaltotal 91.6% 198 / 216 CC 17 SCAR 5.5
CC
17
Cov
95%
__ANON__
4.0
gkseq
3.0
__ANON__
3.0
CRAP 1.7
« CPS/Governor/Deferred.pm ↑ Summary CPS/Governor.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, 2008-2010 -- leonerd@leonerd.org.uk
5  
6 package CPS;
7  
8 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 our $VERSION = '0.19';
12  
13 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 16 our @CPS_PRIMS = qw(
16 kloop
17 kwhile
18 kforeach
19 kdescendd kdescendb
20  
21 kpar
22 kpareach
23  
24 kseq
25 );
26  
27 16 our @EXPORT_OK = (
28 @CPS_PRIMS,
29 map( "g$_", @CPS_PRIMS ),
30  
31 qw(
32 liftk
33 dropk
34 ),
35 );
36  
37 16 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 16 ▶use CPS::Governor::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # Don't hard-depend on Sub::Name since it's only a niceness for stack traces
42 16 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.4
43 16 ▶ if( eval { require Sub::Name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Sub::Name } }) { } 16 0 16
44 16 *subname = \&Sub::Name::subname;
45 }
46 else {
47 # Ignore the name, return the CODEref
48 0 ▶ *subname = sub { return $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 }
50 }
51  
52 =head1 NAME
53  
54 C<CPS> - manage flow of control in Continuation-Passing Style
55  
56 =head1 OVERVIEW
57  
58 =over 4
59  
60 B<Note>: This module is entirely deprecated now. It is maintained for
61 compatibility for any code still using it, but please consider rewriting to
62 use L<Future> instead, which offers a far neater method of representing
63 asynchronous program and data flow. In addition, L<Future::AsyncAwait> can
64 further improve readability of C<Future>-based code by letting it use the
65 familiar kinds of Perl control structure while still being asynchronous.
66  
67 At some later date this entire C<CPS> module distribution may be deleted.
68  
69 =back
70  
71 The functions in this module implement or assist the writing of programs, or
72 parts of them, in Continuation Passing Style (CPS). Briefly, CPS is a style
73 of writing code where the normal call/return mechanism is replaced by explicit
74 "continuations", values passed in to functions which they should invoke, to
75 implement return behaviour. For more detail on CPS, see the SEE ALSO section.
76  
77 What this module implements is not in fact true CPS, as Perl does not natively
78 support the idea of a real continuation (such as is created by a co-routine).
79 Furthermore, for CPS to be efficient in languages that natively support it,
80 their runtimes typically implement a lot of optimisation of CPS code, which
81 the Perl interpreter would be unable to perform. Instead, CODE references are
82 passed around to stand in their place. While not particularly useful for most
83 regular cases, this becomes very useful whenever some form of asynchronous or
84 event-based programming is being used. Continuations passed in to the body
85 function of a control structure can be stored in the event handlers of the
86 asynchronous or event-driven framework, so that when they are invoked later,
87 the code continues, eventually arriving at its final answer at some point in
88 the future.
89  
90 In order for these examples to make sense, a fictional and simple
91 asynchronisation framework has been invented. The exact details of operation
92 should not be important, as it simply stands to illustrate the point. I hope
93 its general intention should be obvious. :)
94  
95 read_stdin_line( \&on_line ); # wait on a line from STDIN, then pass it
96 # to the handler function
97  
98 This module itself provides functions that manage the flow of control through
99 a continuation passing program. They do not directly facilitate the flow of
100 data through a program. That can be managed by lexical variables captured by
101 the closures passed around. See the EXAMPLES section.
102  
103 For CPS versions of data-flow functionals, such as C<map> and C<grep>, see
104 also L<CPS::Functional>.
105  
106 =head1 SYNOPSIS
107  
108 use CPS qw( kloop );
109  
110 kloop( sub {
111 my ( $knext, $klast ) = @_;
112  
113 print "Enter a number, or q to quit: ";
114  
115 read_stdin_line( sub {
116 my ( $first ) = @_;
117 chomp $first;
118  
119 return $klast->() if $first eq "q";
120  
121 print "Enter a second number: ";
122  
123 read_stdin_line( sub {
124 my ( $second ) = @_;
125  
126 print "The sum is " . ( $first + $second ) . "\n";
127  
128 $knext->();
129 } );
130 } );
131 },
132 sub { exit }
133 );
134  
135 =cut
136  
137 =head1 FUNCTIONS
138  
139 In all of the following functions, the C<\&body> function can provide results
140 by invoking its continuation / one of its continuations, either synchronously
141 or asynchronously at some point later (via some event handling or other
142 mechanism); the next invocation of C<\&body> will not take place until the
143 previous one exits if it is done synchronously.
144  
145 They all take the prefix C<k> before the name of the regular perl keyword or
146 function they aim to replace. It is common in CPS code in other languages,
147 such as Scheme or Haskell, to store a continuation in a variable called C<k>.
148 This convention is followed here.
149  
150 =cut
151  
152 =head2 kloop( \&body, $k )
153  
154 CPS version of perl's C<while(true)> loop. Repeatedly calls the C<body> code
155 until it indicates the end of the loop, then invoke C<$k>.
156  
157 $body->( $knext, $klast )
158 $knext->()
159 $klast->()
160  
161 $k->()
162  
163 If C<$knext> is invoked, the body will be called again. If C<$klast> is
164 invoked, the continuation C<$k> is invoked.
165  
166 =head2 kwhile( \&body, $k )
167  
168 Compatibility synonym for C<kloop>; it was renamed after version 0.10. New
169 code should use C<kloop> instead.
170  
171 =cut
172  
173 sub _fix
174 112 ▶{
Subroutine _fix: called CC=1 SCAR=0.0
175 112 my ( $func ) = @_;
176 89 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
177 89 unshift @_, _fix( $func );
178 89 goto &$func;
179 112 };
180 }
181  
182 sub gkloop
183 23 ▶{
Subroutine gkloop: called CC=2 SCAR=6.9
Pod undocumented
184 23 my ( $gov, $body, $k ) = @_;
185  
186 # We can't just call this as a method because we need to tailcall it
187 # Instead, keep a reference to the actual method so we can goto &$enter
188 23 ▶ my $enter = $gov->can('enter') or croak "Governor cannot ->enter";
Branch: 1 branch
BranchTrueFalseTotal
my $enter = $gov->can("enter") or croak("Governor cannot ->enter") 23 0 23
189  
190 89 ▶ my $kfirst = _fix subname gkloop => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
191 89 my $knext = shift;
192  
193 89 my $sync = 1;
194 89 my $do_again;
195 $enter->( $gov, $body,
196 66 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
197 66 ▶ if( $sync ) { $do_again=1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($sync) { } 60 6 66
198 6 else { goto &$knext; }
199 },
200 23 ▶ sub { @_ = (); goto &$k },
Subroutine __ANON__: called CC=1 SCAR=0.0
201 89 );
202 89 $sync = 0;
203  
204 89 ▶ if( $do_again ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_again) 60 29 89
205 60 $do_again = 0;
206 60 goto &$knext;
207 }
208 23 };
209  
210 23 goto &$kfirst;
211 }
212  
213 16 *gkwhile = \&gkloop;
214  
215 =head2 kforeach( \@items, \&body, $k )
216  
217 CPS version of perl's C<foreach> loop. Calls the C<body> code once for each
218 element in C<@items>, until either the items are exhausted or the C<body>
219 invokes its C<$klast> continuation, then invoke C<$k>.
220  
221 $body->( $item, $knext, $klast )
222 $knext->()
223 $klast->()
224  
225 $k->()
226  
227 =cut
228  
229 sub gkforeach
230 10 ▶{
Subroutine gkforeach: called CC=1 SCAR=0.0
Pod undocumented
231 10 my ( $gov, $items, $body, $k ) = @_;
232  
233 10 my $idx = 0;
234  
235 gkloop( $gov,
236 35 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
237 35 my ( $knext, $klast ) = @_;
238 35 ▶ goto &$klast unless $idx < scalar @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless $idx < scalar @$items 9 26 35
239 26 @_ =(
240 $items->[$idx++],
241 $knext,
242 $klast
243 );
244 26 goto &$body;
245 },
246 10 $k,
247 );
248 }
249  
250 =head2 kdescendd( $root, \&body, $k )
251  
252 CPS version of recursive descent on a tree-like structure, defined by a
253 function, C<body>, which when given a node in the tree, yields a list of
254 child nodes.
255  
256 $body->( $node, $kmore )
257 $kmore->( @child_nodes )
258  
259 $k->()
260  
261 The first value to be passed into C<body> is C<$root>.
262  
263 At each iteration, a node is given to the C<body> function, and it is expected
264 to pass a list of child nodes into its C<$kmore> continuation. These will then
265 be iterated over, in the order given. The tree-like structure is visited
266 depth-first, descending fully into one subtree of a node before moving on to
267 the next.
268  
269 This function does not provide a way for the body to accumulate a resultant
270 data structure to pass into its own continuation. The body is executed simply
271 for its side-effects and its continuation is invoked with no arguments. A
272 variable of some sort should be shared between the body and the continuation
273 if this is required.
274  
275 =cut
276  
277 sub gkdescendd
278 1 ▶{
Subroutine gkdescendd: called CC=1 SCAR=0.0
Pod undocumented
279 1 my ( $gov, $root, $body, $k ) = @_;
280  
281 1 my @stack = ( $root );
282  
283 gkloop( $gov,
284 9 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
285 9 my ( $knext, $klast ) = @_;
286 @_ = (
287 shift @stack,
288 9 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
289 9 unshift @stack, @_;
290  
291 9 ▶ goto &$knext if @stack;
Branch: 1 branch
BranchTrueFalseTotal
if @stack 8 1 9
292 1 goto &$klast;
293 },
294 9 );
295 9 goto &$body;
296 },
297 1 $k,
298 );
299 }
300  
301 =head2 kdescendb( $root, \&body, $k )
302  
303 A breadth-first variation of C<kdescendd>. This function visits each child
304 node of the parent, before iterating over all of these nodes's children,
305 recursively until the bottom of the tree.
306  
307 =cut
308  
309 sub gkdescendb
310 1 ▶{
Subroutine gkdescendb: called CC=1 SCAR=0.0
Pod undocumented
311 1 my ( $gov, $root, $body, $k ) = @_;
312  
313 1 my @queue = ( $root );
314  
315 gkloop( $gov,
316 9 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
317 9 my ( $knext, $klast ) = @_;
318 @_ = (
319 shift @queue,
320 9 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
321 9 push @queue, @_;
322  
323 9 ▶ goto &$knext if @queue;
Branch: 1 branch
BranchTrueFalseTotal
if @queue 8 1 9
324 1 goto &$klast;
325 },
326 9 );
327 9 goto &$body;
328 },
329 1 $k,
330 );
331 }
332  
333 =head2 kpar( @bodies, $k )
334  
335 This CPS function takes a list of function bodies and calls them all
336 immediately. Each is given its own continuation. Once every body has invoked
337 its continuation, the main continuation C<$k> is invoked.
338  
339 $body->( $kdone )
340 $kdone->()
341  
342 $k->()
343  
344 This allows running multiple operations in parallel, and waiting for them all
345 to complete before continuing. It provides in a CPS form functionality
346 similar to that provided in a more object-oriented fashion by modules such as
347 L<Async::MergePoint> or L<Event::Join>.
348  
349 =cut
350  
351 sub gkpar
352 5 ▶{
Subroutine gkpar: called CC=2 SCAR=6.9
Pod undocumented
353 5 my ( $gov, @bodies ) = @_;
354 5 my $k = pop @bodies;
355  
356 5 ▶ $gov->can('enter') or croak "Governor cannot ->enter";
Branch: 1 branch
BranchTrueFalseTotal
$gov->can("enter") or croak("Governor cannot ->enter") 5 0 5
357  
358 5 my $sync = 1;
359 5 my @outstanding;
360 14 ▶ my $kdone = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
361 14 ▶ return if $sync;
Branch: 1 branch
BranchTrueFalseTotal
if $sync 5 9 14
362 9 ▶ $_ and return for @outstanding;
Branch: 1 branch
BranchTrueFalseTotal
$_ and return 4 11 15
363 5 goto &$k;
364 5 };
365  
366 gkforeach( $gov, [ 0 .. $#bodies ],
367 9 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
368 9 my ( $idx, $knext ) = @_;
369 9 $outstanding[$idx]++;
370 9 ▶ $gov->enter( $bodies[$idx], sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
371 9 $outstanding[$idx]--;
372 9 @_ = ();
373 9 goto &$kdone;
374 9 } );
375 9 goto &$knext;
376 },
377 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
378 5 $sync = 0;
379 5 @_ = ();
380 5 goto &$kdone;
381 }
382 5 );
383 }
384  
385 =head2 kpareach( \@items, \&body, $k )
386  
387 This CPS function takes a list of items and a function body, and calls the
388 body immediately once for each item in the list. Each invocation is given its
389 own continuation. Once every body has invoked its continuation, the main
390 continuation C<$k> is invoked.
391  
392 $body->( $item, $kdone )
393 $kdone->()
394  
395 $k->()
396  
397 This is similar to C<kforeach>, except that the body is started concurrently
398 for all items in the list list, rather than each item waiting for the previous
399 to finish.
400  
401 =cut
402  
403 sub gkpareach
404 2 ▶{
Subroutine gkpareach: called CC=1 SCAR=0.0
Pod undocumented
405 2 my ( $gov, $items, $body, $k ) = @_;
406  
407 gkpar( $gov,
408 (map {
409 2 my $item = $_;
410 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
411 4 unshift @_, $item;
412 4 goto &$body
413 }
414 4 } @$items),
415 $k
416 );
417 }
418  
419 =head2 kseq( @bodies, $k )
420  
421 This CPS function takes a list of function bodies and calls them each, one at
422 a time in sequence. Each is given a continuation to invoke, which will cause
423 the next body to be invoked. When the last body has invoked its continuation,
424 the main continuation C<$k> is invoked.
425  
426 $body->( $kdone )
427 $kdone->()
428  
429 $k->()
430  
431 A benefit of this is that it allows a long operation that uses many
432 continuation "pauses", to be written without code indenting further and
433 further to the right. Another is that it allows easy skipping of conditional
434 parts of a computation, which would otherwise be tricky to write in a CPS
435 form. See the EXAMPLES section.
436  
437 =cut
438  
439 sub gkseq
440 2 ▶{
Subroutine gkseq: called CC=3 SCAR=11.0
Pod undocumented
441 2 my ( $gov, @bodies ) = @_;
442 2 my $k = pop @bodies;
443  
444 2 ▶ my $enter = $gov->can('enter') or croak "Governor cannot ->enter";
Branch: 1 branch
BranchTrueFalseTotal
my $enter = $gov->can("enter") or croak("Governor cannot ->enter") 2 0 2
445  
446 2 ▶ while( @bodies ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bodies) 4 2 6
447 4 my $nextk = $k;
448 4 my $b = pop @bodies;
449 4 ▶ $k = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
450 4 @_ = ( $gov, $b, $nextk );
451 4 goto &$enter;
452 4 };
453 }
454  
455 2 @_ = ();
456 2 goto &$k;
457 }
458  
459 =head1 GOVERNORS
460  
461 All of the above functions are implemented using a loop which repeatedly calls
462 the body function until some terminating condition. By controlling the way
463 this loop re-invokes itself, a program can control the behaviour of the
464 functions.
465  
466 For every one of the above functions, there also exists a variant which takes
467 a L<CPS::Governor> object as its first argument. These functions use the
468 governor object to control their iteration.
469  
470 kloop( \&body, $k )
471 gkloop( $gov, \&body, $k )
472  
473 kforeach( \@items, \&body, $k )
474 gkforeach( $gov, \@items, \&body, $k )
475  
476 etc...
477  
478 In this way, other governor objects can be constructed which have different
479 running properties; such as interleaving iterations of their loop with other
480 IO activity in an event-driven framework, or giving rate-limitation control on
481 the speed of iteration of the loop.
482  
483 =cut
484  
485 # The above is a lie. The basic functions provided are actually the gk*
486 # versions; we wrap these to make the normal k* functions by passing a simple
487 # governor.
488 sub _governate
489 153 ▶{
Subroutine _governate: called CC=2 SCAR=7.1
490 153 my $pkg = caller;
491 153 my ( $func, $name ) = @_;
492  
493 153 my $default_gov = CPS::Governor::Simple->new;
494  
495 16 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
496  
497 153 ▶ my $code = $pkg->can( $func ) or croak "$pkg cannot $func()";
Branch: 1 branch
BranchTrueFalseTotal
my $code = $pkg->can($func) or croak("$pkg cannot $func()") 153 0 153
498 153 ▶ *{$pkg."::$name"} = subname $name => sub {
Subroutine kdescendd: not called CC=1 SCAR=0.6
Subroutine kforeach: called CC=1 SCAR=0.6
Pod documented
Pod documented
499 0 unshift @_, $default_gov;
500 0 goto &$code;
501 153 };
502 }
503  
504 16 _governate "g$_" => $_ for @CPS_PRIMS;
505  
506 =head1 CPS UTILITIES
507  
508 These function names do not begin with C<k> because they are not themselves
509 CPS primatives, but may be useful in CPS-oriented code.
510  
511 =cut
512  
513 =head2 $kfunc = liftk { BLOCK }
514  
515 =head2 $kfunc = liftk( \&func )
516  
517 Returns a new CODE reference to a CPS-wrapped version of the code block or
518 passed CODE reference. When C<$kfunc> is invoked, the function C<&func> is
519 called in list context, being passed all the arguments given to C<$kfunc>
520 apart from the last, expected to be its continuation. When C<&func> returns,
521 the result is passed into the continuation.
522  
523 $kfunc->( @func_args, $k )
524 $k->( @func_ret )
525  
526 The following are equivalent
527  
528 print func( 1, 2, 3 );
529  
530 my $kfunc = liftk( \&func );
531 $kfunc->( 1, 2, 3, sub { print @_ } );
532  
533 Note that the returned wrapper function only has one continuation slot in its
534 arguments. It therefore cannot be used as the body for C<kloop()>,
535 C<kforeach()> or C<kgenerate()>, because these pass two continuations. There
536 does not exist a "natural" way to lift a normal call/return function into a
537 CPS function which requires more than one continuation, because there is no
538 way to distinguish the different named returns.
539  
540 =cut
541  
542 sub liftk(&)
543 3 ▶{
Subroutine liftk: called CC=1 SCAR=0.0
Pod documented
544 3 my ( $code ) = @_;
545  
546 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
547 3 my $k = pop;
548 3 @_ = $code->( @_ );
549 3 goto &$k;
550 3 };
551 }
552  
553 =head2 $func = dropk { BLOCK } $kfunc
554  
555 =head2 $func = dropk $waitfunc, $kfunc
556  
557 Returns a new CODE reference to a plain call/return version of the passed
558 CPS-style CODE reference. When the returned ("dropped") function is called,
559 it invokes the passed CPS function, then waits for it to invoke its
560 continuation. When it does, the list that was passed to the continuation is
561 returned by the dropped function. If called in scalar context, only the first
562 value in the list is returned.
563  
564 $kfunc->( @func_args, $k )
565 $k->( @func_ret )
566  
567 $waitfunc->()
568  
569 @func_ret = $func->( @func_args )
570  
571 Given the following trivial CPS function:
572  
573 $kadd = sub { $_[2]->( $_[0] + $_[1] ) };
574  
575 The following are equivalent
576  
577 $kadd->( 10, 20, sub { print "The total is $_[0]\n" } );
578  
579 $add = dropk { } $kadd;
580 print "The total is ".$add->( 10, 20 )."\n";
581  
582 In the general case the CPS function hasn't yet invoked its continuation by
583 the time it returns (such as would be the case when using any sort of
584 asynchronisation or event-driven framework). For C<dropk> to actually work in
585 this situation, it requires a way to run the event framework, to cause it to
586 process events until the continuation has been invoked.
587  
588 This is provided by the block, or the first passed CODE reference. When the
589 returned function is invoked, it repeatedly calls the block or wait function,
590 until the CPS function has invoked its continuation.
591  
592 =cut
593  
594 sub dropk(&$)
595 2 ▶{
Subroutine dropk: called CC=1 SCAR=0.0
Pod documented
596 2 my ( $waitfunc, $kfunc ) = @_;
597  
598 3 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
599 3 my @result;
600 my $done;
601  
602 3 ▶ $kfunc->( @_, sub { @result = @_; $done = 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
603  
604 3 ▶ while( !$done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 2 3 5
605 2 $waitfunc->();
606 }
607  
608 3 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
609 }
610 2 }
611  
612 =head1 EXAMPLES
613  
614 =head2 Returning Data From Functions
615  
616 No facilities are provided directly to return data from CPS body functions in
617 C<kloop>, C<kpar> and C<kseq>. Instead, normal lexical variable capture may
618 be used here.
619  
620 my $bat;
621 my $ball;
622  
623 kpar(
624 sub {
625 my ( $k ) = @_;
626 get_bat( on_bat => sub { $bat = shift; goto &$k } );
627 },
628 sub {
629 my ( $k ) = @_;
630 serve_ball( on_ball => sub { $ball = shift; goto &$k } );
631 },
632  
633 sub {
634 $bat->hit( $ball );
635 },
636 );
637  
638 The body function can set the value of a variable that it and its final
639 continuation both capture.
640  
641 =head2 Using C<kseq> For Conditionals
642  
643 Consider the call/return style of code
644  
645 A();
646 if( $maybe ) {
647 B();
648 }
649 C();
650  
651 We cannot easily write this in CPS form without naming C twice
652  
653 kA( sub {
654 $maybe ?
655 kB( sub { kC() } ) :
656 kC();
657 } );
658  
659 While not so problematic here, it could get awkward if C were in fact a large
660 code block, or if more than a single conditional were employed in the logic; a
661 likely scenario. A further issue is that the logical structure becomes much
662 harder to read.
663  
664 Using C<kseq> allows us to name the continuation so each arm of C<kmaybe> can
665 invoke it indirectly.
666  
667 kseq(
668 \&kA,
669 sub { my $k = shift; $maybe ? kB( $k ) : goto &$k; },
670 \&kC
671 );
672  
673 =head1 SEE ALSO
674  
675 =over 4
676  
677 =item *
678  
679 L<Future> - represent an operation awaiting completion
680  
681 =item *
682  
683 L<Future::AsyncAwait> - deferred subroutine syntax for futures
684  
685 =item *
686  
687 L<CPS::Functional> - functional utilities in Continuation-Passing Style
688  
689 =item *
690  
691 L<http://en.wikipedia.org/wiki/Continuation-passing_style> on wikipedia
692  
693 =back
694  
695 =head1 ACKNOWLEDGEMENTS
696  
697 Matt S. Trout (mst) <mst@shadowcat.co.uk> - for the inspiration of C<kpareach>
698 and with apologies to for naming of the said. ;)
699  
700 =head1 AUTHOR
701  
702 Paul Evans <leonerd@leonerd.org.uk>
703  
704 =cut
705  
706 16 0x55AA;
« CPS/Governor/Deferred.pm ↑ Summary CPS/Governor.pm »