perlquestion
leriksen
I have been rereading an old (1989) MC68000 programming text [isbn://0471500216|Microcomputer Architecture and Programming ] and the author introduces a concept called "coroutines"
<p>
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.
<p>
Some ASCII art may help.
<p>
In pseudo pascal...
<code>
coroutine cor1 { coroutine cor2 {
BEGIN
...
RESUME cor2;--------->BEGIN
...
...<--------------------RESUME cor1;
...
RESUME cor2;----------->...
...
...<--------------------RESUME cor1;
...
RESUME cor2;----------->...
...
...<--------------------RESUME cor1;
... END;
END; }
}
</code>
<p>
Coroutines preserve the values of their local variables between successive calls (like statics in C).
<p>
Coroutines appear to be like subroutines with multiple entry (and exit) points
<p>
Coroutines were first mentioned by [isbn://0201896834|Knuth] in 1973, who says he found the concept mentioned in a 1954 UNIVAC programming tip !
<p>
My question is - could (and should) you implement coroutines in Perl ?
<p>
My feeling is yes it could, but not in an obvious way. And that even though you could, you shouldn't.
<p>
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'
<p>Update 07052004-18:42:01- added name of original text
<div class="pmsig">
<div class="pmsig-189168">
<p>+++++++++++++++++
<br>#!/usr/bin/perl
<br>use warnings;use strict;use brain;
</div></div>