|Perl: the Markov chain saw|
Coroutines in Perlby leriksen (Curate)
|on May 07, 2004 at 06:09 UTC||Need Help??|
leriksen has asked for the
wisdom of the Perl Monks concerning the following question:
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...
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