Subject: | Iterator calculates next value too soon. |
Iterator assumes that the sub used to calculate its next value is pure,
and pre-fetches the next value before it's requested.
If the sub is not pure, Iterator then returns the wrong values, as
demonstrated by the following test file. The first two tests work fine,
but the third test, which should return 21, returns the next value in
the original sequence (since it was fetched before we changed the
variable $value). The last test fails, returning the value we would have
expected for the 3rd test.
This is blocking an update to the svn-binary-search module.
You're free to use this test file in your module.
Regards.
$ cat foo.pl
#! /usr/local/bin/perl
use strict;
use warnings;
use Iterator;
use Test::More;
my $value = 0;
my $foo = Iterator->new( sub { return ++$value } );
plan(tests=>4);
is($foo->value(),1);
is($foo->value(),2);
# Reset value we're keying off to something else entirely.
$value = 20;
is($foo->value(),21);
is($foo->value(),22);