Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I have been rereading an old (1989) MC68000 programming text Microcomputer Architecture and Programming and the author introduces a concept called "coroutines"

These, as far as I can tell are like subroutines, but they contain goto's that _move_ between invocations of the sub. When the sub is reinvoked, execution resumes at the new goto position, not the start.

Some ASCII art may help.

In pseudo pascal...

coroutine cor1 { coroutine cor2 { BEGIN ... RESUME cor2;--------->BEGIN ... ...<--------------------RESUME cor1; ... RESUME cor2;----------->... ... ...<--------------------RESUME cor1; ... RESUME cor2;----------->... ... ...<--------------------RESUME cor1; ... END; END; } }

Coroutines preserve the values of their local variables between successive calls (like statics in C).

Coroutines appear to be like subroutines with multiple entry (and exit) points

Coroutines were first mentioned by Knuth in 1973, who says he found the concept mentioned in a 1954 UNIVAC programming tip !

My question is - could (and should) you implement coroutines in Perl ?

My feeling is yes it could, but not in an obvious way. And that even though you could, you shouldn't.

I also feel that I would seriously question code that intended to use them - they seem unsafe from a reentrant point of view, and definitely tricky in threaded code. Debugging coroutine based code would seem to be 'fun', for exceedingly small values of 'fun'

Update 07052004-18:42:01- added name of original text

use warnings;use strict;use brain;

In reply to Coroutines in Perl by leriksen

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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-12-02 10:39 GMT
Find Nodes?
    Voting Booth?