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)
package Tree;
sub next_inorder{
my $self = persist();
yield { $self->{left}->next_inorder } if $self->{left}
+;
yield { $self };
yield { $self->{right}->next_inorder if } $self->{righ
+t};
yield_return undef;
}
# and later...
while (my $node = $root->next_inorder()) {
print $node->{data};
}
Anyone see a flaw or potential snags with this attack?
-Lee
"To be civilized is to deny one's nature."
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.