This and other RFCs are available on the web at
  http://tmtowtdi.perl.org/rfc/

=head1 TITLE

Coroutines for Perl

=head1 VERSION

  Maintainer: Tom Scola <[EMAIL PROTECTED]>
  Date: 04 Aug 2000
  Version: 1
  Mailing List: [EMAIL PROTECTED]
  Number: 27

=head1 ABSTRACT

Two motavations for the development of Perl 6 are to bring threading
and event-based programming to the core language.  Coroutines can be
implemented in either a threaded or an event-based manner, are at a
much higher level conceptually, and are much easier to program than
either of those methods.  As implemented in perl, coroutines use
programming methods that most perl programmers are already familiar
with, making the transition to them simple and natural.

=head1 DESCRIPTION

This proposal introduces a new keyword to perl, "co", as a complement
to "sub".  A subroutine is defined thusly:

  co foo { ... }

Coroutines can also be closures:

  my $x = co { ... }

Unlike coroutines as defined by Knuth, and implemented in laguages
such as Simula or Python, perl does not have an
explicit "resume" call for invoking coroutines.  Instead, perl uses a
mechanism that perl programmers are already familiar with: pipes.
Note that these are intra-process pipes, and would be implemented
differently than kernel pipes.

Coroutines are identified by a prefixed vertical bar.  To invoke a
coroutine, you write data to or read data from it.

   $y = <|foo>;

   print |$x "hello, world\n";

Inside a coroutine, the meanings of "<>" and the default file
descriptor for print, printf, etc. are overloaded.

Coroutines and pipelines would effectively turn Perl into a dataflow
programming language, providing yet another paradigm for Perl to
encompass.

=head1 IMPLEMENTATION

Coroutines can be implemented in their traditional manner, using context
switching, or as threads, but would hide the mundane details of threads
from the programmer.  They would be more suited for producer/consumer type
problems rather than massively parallel compuations, but I doubt there is
much call for the latter to be implemented in perl.

=head2 OPEN ISSUES

=over

=item B<coroutine priority>

There probably needs to be some mechanism for defining the priority of
a coroutine.

=item B<reference passing>

Is new syntax needed for passing references via pipelines?

=back

=head1 REFERENCES

=over

=item *

Donald Knuth, I<The Art of Computer Programming>

=item *

"Pipes -- Powerful and Elegant Programming Paradigm" 
http://www.softpanorama.org/Scripting/pipes.shtml

=item *

"Coroutines in Python" http://www.nightmare.com/~rushing/copython/

=back



Reply via email to