CPS

Flow control structures in Continuation-Passing Style
Download

CPS Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Paul Evans
  • Publisher web site:
  • http://search.cpan.org/~pevans/

CPS Tags


CPS Description

Flow control structures in Continuation-Passing Style The functions in the CPS Perl module implement or assist the writing of programs, or parts of them, in Continuation Passing Style (CPS). Briefly, CPS is a style of writing code where the normal call/return mechanism is replaced by explicit "continuations", values passed in to functions which they should invoke, to implement return behaviour. For more detail on CPS, see the SEE ALSO section.What this module implements is not in fact true CPS, as Perl does not natively support the idea of a real continuation (such as is created by a co-routine). Furthermore, for CPS to be efficient in languages that natively support it, their runtimes typically implement a lot of optimisation of CPS code, which the Perl interpreter would be unable to perform. Instead, CODE references are passed around to stand in their place. While not particularly useful for most regular cases, this becomes very useful whenever some form of asynchronous or event-based programming is being used. Continuations passed in to the body function of a control structure can be stored in the event handlers of the asynchronous or event-driven framework, so that when they are invoked later, the code continues, eventually arriving at its final answer at some point in the future.In order for these examples to make sense, a fictional and simple asynchronisation framework has been invented. The exact details of operation should not be important, as it simply stands to illustrate the point. I hope its general intention should be obvious. :) read_stdin_line( \&on_line ); # wait on a line from STDIN, then pass it # to the handler functionSYNOPSIS use CPS qw( kwhile ); kwhile( sub { my ( $knext, $klast ) = @_; print "Enter a number, or q to quit: "; read_stdin_line( sub { my ( $first ) = @_; chomp $first; return $klast->() if $first eq "q"; print "Enter a second number: "; read_stdin_line( sub { my ( $second ) = @_; print "The sum is " . ( $first + $second ) . "\n"; $knext->(); } ); } ); }, sub { exit } ); Requirements: · Perl


CPS Related Software