Luke Palmer wrote:

I just need a little clarification about yield().
The first point of clarification is that the subject is a little off.
C<yield> gives us *co-routines*, not *continuations*.


consider this sub:

sub iterate(@foo) {
yield for @foo;
undef;
}

(Where yield defaults to the topic) Presumably.
I'm not so sure. C<return> doesn't default to topic; C<yield> probably
shouldn't either.


@a = (1, 2, 3, 4, 5);
while($_ = iterate @a) {
print
}

Will print "12345".
Yes.


> So, does yield() build a lazy array, or does it act like an eplicit
iterator?
The latter. C<yield> is exactly like a C<return>, except that when you
call the subroutine next time, it resumes from after the C<yield>.


If the latter,  how do you tell the difference between a
recursive call and fetching the next element?  How would you maintain
two iterators into the same array?
The re-entry point isn't stored in the subroutine itself. It's stored
(indexed by optree node) in the current subroutine call frame. Which,
of course, is preserved when recursive iterator invocations recursively
yield.

Damian

Reply via email to