B
Brad Baxter
The code below ............
use strict;
use warnings;
use Array::Each qw/them rewind/;
$|=1;
while (1) {
my @array = map{rand}(1..10);
while (my ($k,$v) = them(@array)) {
print "[$k:$v]";
last if ($k == 0);
}
print "\nnext -> ";
### rewind(@array); # would fix it
}
produces..............
next -> [1:0][2:8]
next -> [0:4]
next -> [1:4][2:0]
next -> [0:1]
next -> [1:0][2:3]
next -> [0:0]
next -> [1:9][2:9]
next -> [0:1]
next -> [1:6][2:4]
next -> [0:1]
next -> [1:0][2:7]
next -> [0:0]
Hmmm, that's not what it produces for me, but I think I catch your drift.
I realize that this occurs, because the variable has the same
reference
even though it has completely changed. You might want to note
something
about breaking out of iteration and how you should call rewind if you
are
going to be using the same variable in a loop.
Good point. I think the OO interface would make this moot (meaning that
the scope of the blessed reference should determine the life of the
iterator), but there may be similar situations that the documentation
might make hints about.
Anyway, I could see how someone might write code that leaves entries
in your %set variable which are never deleted. This might be a
problem for a daemon.
Yes. I think the OO interface should take care of this, too.
Perhaps, some type of syntax as shown below would work.....
Without keeping a global hash.......
Of course it would have to be modified to iterate over multiple
arrays.
(I like your syntax better, I just don't like the way context is
maintained.
Although I don't see any better way to do it, while keeping your
syntax.)
I'm fond of "my syntax" , too, but I think OO will win the day.
I really don't like the idea of the function being named each, but
that is just my opinion.
Since OO won't export it, I'm still leaning toward 'each'.
Thanks a bunch for the feedback,
Brad