Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found