According to
http://search.cpan.org/~flora/Moose-1.01/lib/Moose/Meta/Attribute/Native/Trait/Array.pm

"natatime($n, $code)

    This method returns an iterator which, on each call, returns $n more
items from the array, in order, like natatime from List::MoreUtils. A
coderef can optionally be provided; it will be called on each group of $n
elements in the array.
"

However, the coderef option doesn't work as expected.


== Testing script:

package AAA;

use Moose;

has attr => (
    isa => 'ArrayRef',
    traits =>  ['Array'],
    default => sub { [1,2,3,4] },
    handles => {
        get_iter => [ natatime => 2 ],
    }
);

has attr2 => (
    isa => 'ArrayRef',
    traits =>  ['Array'],
    default => sub { [1,2,3,4] },
    handles => {
        get_iter2 => [ natatime => 2 => sub {} ],
    }
);
package main;

my $r = AAA->new;
my $it = $r->get_iter;
while (my @vars = $it->()) {
    print "iter: ", @vars, "\n";
}
my $it2 = $r->get_iter2;
while (my @vars = $it2->()) {
    print "iter2: ", @vars, "\n";
}
==

Output:

$ perl test.pl
iter: 12
iter: 34
$

===

The reason why there's no output from it2 is that iterator has been
exhausted by "sub {}" already.

See code from:
http://cpansearch.perl.org/src/FLORA/Moose-1.01/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm

sub natatime : method {
    my ( $attr, $reader, $writer ) = @_;
    return sub {
        my ( $instance, $n, $f ) = @_;
        my $it = List::MoreUtils::natatime($n, @{ $reader->($instance) });
        if ($f) {
            while (my @vals = $it->()) {
                $f->(@vals);
            }
        }
        $it;
    };
}


http://cpansearch.perl.org/src/VPARSEVAL/List-MoreUtils-0.22/lib/List/MoreUtils.pm
sub natatime ($@)
{
    my $n = shift;
    my @list = @_;

    return sub
    {
        return splice @list, 0, $n;
    }
}

==

Hope someone could fix this bug.

Regards,
Justin

Reply via email to