On Wed, 06 Nov 2002 10:38:45 +1100, Damian Conway wrote:
> Luke Palmer wrote:
> > I just need a little clarification about yield().
> 
> C<yield> is exactly like a C<return>, except that when you
> call the subroutine next time, it resumes from after the C<yield>.
> 
> > 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.

So to get the same yield context, each call to the coroutine has to be from
the same calling frame. If you want to get several values from the same
coroutine, but from different calling contexts, can you avoid the need to
wrap it in a closure?

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

  # There's probably some perl5/6 confusion here
  sub consume(@bar){
    my $next = sub{ iterate(@bar); };
    while $_ = $next() {
      do_stuff($_,$next);
    }
  }

  sub do_stuff($val,$next){
    ...
    if $val ~~ something_or_other() {
      my $quux = $next();
      ...
    }
  }


-- 
        Peter Haworth   [EMAIL PROTECTED]
"...I find myself wondering if Larry Ellison and Tim Curry
 were separated at birth...hmm..."
                -- Tom Good

Reply via email to