Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Magic::Attach & Co-routines?by shotgunefx (Parson) |
on Jan 21, 2003 at 11:38 UTC ( [id://228652]=note: print w/replies, xml ) | Need Help?? |
I wonder if this couldn't be used for implementing co-routines. I started thinking about it and remembered a post by Ovid on the subject.
Here's what I am thinking and I am sure there are flaws and other issues problems I haven't thought of. You use attach_var() to create persistant arguments for the sub. (This could be wrapped in a routine, say persist(); This would just be syntactical sugar for $var = attached(blahblah} or attach_var(blabla); You have a sub called yield that takes a block as an argument. It computes a "context key" from the caller info. It checks to see if it the calling sub has a "context stash" associated with it. This would be an array of contexts. If not it creates one (attached to the calling sub) and pushes an empty context. It could then check to see if had been called already in this context. (With the same args, from the same line in source.) If it has it returns. Basically a NOOP. If it hasn't, it marks itself as being called in the stash and pushes a new context on the "context stack", it then executes the block. You then have a sub called yield_return(). This pops the context off of the stack and returns. It would look a little something like this. (Using TheDamians example at http://www.yetanother.org/damian/Perl5+i/coroutines.html) Anyone see a flaw or potential snags with this attack? -Lee "To be civilized is to deny one's nature."
In Section
Meditations
|
|