in reply to Re: Taken out of Context
in thread Taken out of Context

Firstly, I wanted to say that I enjoyed your article. It's well written and very informative. :-) However, I noticed a small (debatable?) bug in your one_each() function.

The problem is that you don't check to make sure that the function is being called on the same two arrays each time. Consider:

my @a = qw(a0 a1 a2); my @b = qw(b0 b1 b2); my @c = qw(c0 c1 c2 c3 c4); my @d = qw(d0 d1 d2 d3 d4); print '(',join(',', one_each(@a, @b)),")\n"; print '(',join(',', one_each(@a, @b)),")\n"; print '(',join(',', one_each(@c, @d)),")\n"; print '(',join(',', one_each(@c, @d)),")\n";

It prints:

(a0,b0) (a1,b1) (c2,d2) ()

So, even though you're giving it two new arrays in the second two calls, it uses the position and size from the first two calls. I think an easy way to "fix" this would be to store a copy of the references to the arrays in one of your closure variables and compare it to the references passed in each time, resetting if they're different. Or, perhaps, if you didn't want the position to reset even if you use different arrays, you could just not store the sizes but, rather, check them from the arrays themselves each time. That way the sizes would be correct without resetting the position.

*shrug* Up to you, though. Just thought I'd mention it. Again, props for a well done article.

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.