[Phoenix-pm] greetings

Scott Walters scott at illogics.org
Tue Jul 27 16:34:22 CDT 2004


Well, coroutines are a lot like POE (or Event, or Stem), except
when the event happens, rather than another routine being called, the
current is allowed to continue. This means that you don't have to
tuck all of your variables into an object to preserve them,
and you don't have to return out of for() loops, if() statements,
deeply nested function calls, and so on... but the POE stuff
about not shooting yourself in the foot with threads still
applies. And no special version of modules are required,
as with threads.

Here's a little script tht demonstrates continuations:

  use Coro; 
  use Coro::Cont;
          
  use File::Find;
  use Perl6::Variables;
   
  sub get_next_perl :Cont {
      find(sub { 
          return unless m/\.pl$/;
          open my $pl, '<', $_ or return;
          (my $shebang) = <$pl> =~ m/^#!(\S+)/ or return;
          yield $shebang;
      }, '/');
      return undef;
  }

  while(my $_ = get_next_perl()) {
      last unless defined $_; 
      print $_, "\n";
  }

This tries to find all of the Perl itnerpreters on your system that are
in use. Coroutines are used to create detached processes and continuations 
are used to create closely coupled processes. In this case, control
flops back and forth between get_next_perl() and the while(). Even
as control flops back, the lexical variables inside get_next_perl()
keep their value. More importantly, the call to File::Find::find() isn't
exected out of. It would suck if that had to be restarted each time
we wanted to know what interpreter the next file used. Likewise with a
network application, this would be responsive, whereas storing everything
in an array and returning a reference would wait a really long time and then 
suddenly have a whole bunch of data.

Hrm. I should write some examples of gluing POE to Coro. Event handler stubs
could execute a ->transfer() to transfer control to a specific coroutine.
I wonder how Coro::Event does it...

-scott

On  0, Artful <ahenry-pm at artful2099.com> wrote:
> 
> > Anyone into multi-threaded perl programming?  It's something I have no
> > experience in and would love to see an overview if anyone is willing.
> > Just a thought.
> >
> > Bobby
> 
> Threading in perl is cumbersome at best.  I am falling in love with POE.
> http://poe.perl.org/
> 
> At first glance, it can seem complex, but the concept is pretty simple and
> it is easy to pick up.  There are a couple of recent articles at perl.com
> covering it: http://www.perl.com/pub/a/2004/07/02/poeintro.html and
> http://www.perl.com/pub/a/2004/07/22/poe.html
> 
> -Art-
> _______________________________________________
> Phoenix-pm mailing list
> Phoenix-pm at pm.org
> http://www.pm.org/mailman/listinfo/phoenix-pm



More information about the Phoenix-pm mailing list