I suggested something similar to perl5-porters in message <199702071400.JAA00295@anshar.shadow.net>. I was writing a MUD in Perl that needed to multitask programs, and I was considering breaking procedural code into small state machines. This is the sample MUD code I included in the message:

  count = 10000000
  while count--
    say "hello, world!  enter some text: "
    getline some_text
    if some_text eq 'quit'
      last
    endif
  endwhile
  say "\ngoodbye, world!\n"

The idea was to break it into basic blocks-- chunks of code delimited by branches-- and perform implicit yields at each branch. I intended to implement the I/O functions like say() and getline() so they would suspend MUD tasks until the calls had completed.

My intention was to "compile" the above MUD code by first translating it into Perl and then using eval() to "assemble" it into bytecode. Here's the Perl sub that the previous code would have been translated into:

  sub aaaaa {
    # assumes the existence of a tasking/event kernel
    my $task = shift;
    my $namespace = $task->{"namespace"};
    my $ip = $task->{'instruction pointer'}; # state

    # initial entry point
    if ($ip == 0) {
      $namespace->{'count'} = 10000000 ;
      $task->{'instruction pointer'} = 1;
    }
    # top of while loop
    elsif ($ip == 1) {
      if ($namespace->{'count'}--) {
        $task->say(qq(hello, world!  enter some text: ));
        # soft block on 'getline'
        $task->{'blocking'} = 'getline';
        $task->{'instruction pointer'} = 2;
      }
      else {
        $task->{'instruction pointer'} = 3;
      }
    }
    # "return" from getline
    elsif ($ip == 2) {
      $namespace->{'some_text'} = $task->getline();
      if ( $namespace->{'some_text'} eq q(quit) ) {
        $task->{'instruction pointer'} = 3;
      }
      else {
        $task->{'instruction pointer'} = 1;
      }
    }
    # after endwhile
    elsif ($ip == 3) {
      $task->say( qq(\ngoodbye, world!\n) ) ;
      $task->{'instruction pointer'} = -1; # signals end
    }
  }

I thought this was possible in 1997, and I think it may even be practical now that we can manipulate and generate bytecode directly from Perl programs.

-- Rocco Caputo / poe.perl.org


In reply to Re: Coroutines in Perl 5 by rcaputo
in thread Coroutines in Perl 5 by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.