> This RFC proposes that the internal cursor iterated by the C<each> function 
> be attached to the instance of C<each> (i.e. its op-tree node),

In the past, this has been a mistake, because it breaks the identity
of closures.  For example, with your proposal, the following code,
which works now, will no longer work at all:

        %a = ...;
        %b = ...;

        sub make_iterator {
          my $hashref = shift;
          return sub { each %$hashref }
        }

        my $a_iterator = make_iterator(\%a);
        my $b_iterator = make_iterator(\%b);

        for (1 .. 100) { 
          push @a, $a_iterator->();
          push @b, $b_iterator->();  
        }

We want to get the data from %a into @a, and the data from %b into @b.
With your proposal, this code must fail.  The most likely failure mode
is that you get 100 copies of %a's first key and value in @a, and 100
copies of %b's first key and value in @b.
                          
The code fails because you said to attach the iterator state to the op
node, and there is only a single op node here.  Unless that op node
has room for an arbitrarily large number of states, the call to
$b_iterator->() is going to destroy the iterator information that was
saved during the call to $a_iterator->().

The solution to this is that the iterator state should be stored in
the pad for the block in which the each() appears.  The op node can
hold the index of this pad element.  Since the two closures do not
share pads, the code will continue to work.

So your proposal can be saved, but it needs to be fixed.

Mark-Jason Dominus                                               [EMAIL PROTECTED]
I am boycotting Amazon. See http://www.plover.com/~mjd/amazon.html for details.


Reply via email to