in reply to Re: Designing a DWIMish interface for generator function
in thread Designing a DWIMish interface for generator function

I have a pretty clear idea of how to implement it, I'm looking for the best interface resp. usage pattern to distinct between returning single steps and an iterator function.

But I'm confident that the idea in the update is the best way to go.

But please why don't you consider using while ( range 1,9 => my $i ) {...} instead of { my $i; while ( range 1,9 => $i ) {...} }?

Cheers Rolf

Replies are listed 'Best First'.
Re^3: Designing a DWIMish interface for generator function
by ikegami (Patriarch) on Feb 01, 2010 at 04:27 UTC

    You need something external to the loop to keep the state.

    You could use a combination of the address of the calling opcode as a key to get the appropriate state, but you'd have no way to reset to the counter if you did that, so you wouldn't be able to last/return/die from within the loop.

    Just look at each for example. To properly use each, you need to do:

    keys %hash; # Reset iterator while (my ($k, $v) = each(%hash)) { ... }

    And even then, it doesn't nest because the iterator is per-hash, not per-each.

      Did you have a look at the code I linked to? It keeps the state, by using the ref of the iterator-variable as key:

      use strict; use warnings; { my %lists; sub deal { my $aref=shift; my $id= \$_[0]; #print $aref,$id; $lists{$id}=$aref if (! exists $lists{$id} ); if (@{$lists{$id}}){ for(@_) { $_=shift @{$lists{$id}}; } return 1; } else { delete $lists{$id}; return 0; } } } $\="\n"; #--------- here we go while (deal [1..9] => my ($a,$b,$c) ){ print $a,$b,$c; while (deal [a=>1,b=>2] => my ($a,$b) ){ print $a,$b; } } #--------- print "EXIT"; __DATA__ 123 a1 b2 456 a1 b2 789 a1 b2

      But I have to admit that till now I haven't thought about last, that's indeed a problem...

      Cheers Rolf

        • You assumed that the iterator variable will be created at the same memory address every pass of the loop. It's a bad assumption. For example, the following code is an infinite loop:

          my @x; while (deal [1..9] => my ($a,$b,$c) ){ print $a,$b,$c; push @x, \$a; }

          Well, maybe not infinite, since deal is leaking memory every pass of the loop...

        • You assumed that the loop will receive a new iterator each time. It's a bad assumption. For example, deal starts at the wrong number the second and third pass of the outer loop in the following code:

          for (1..3) { while (deal [1..9] => my ($a,$b,$c) ){ print $a,$b,$c; last; } }

        Just like you need to call keys before each, you'd need to reset the iterator before entering the loop. You'd need

        init_deal my ($a,$b,$c) while (deal [1..9] => ($a,$b,$c) ){ ... }