MooseX-Iterator reviews

cpanratings
 

RSS | Module Info | Add a review of MooseX-Iterator

MooseX-Iterator (0.11) ****

UPDATE (version 0.11)
I am very grateful that Robert created this. I was about to create my own when I found this.

If you don't plan on using the peek function, everything should behave as you would expect.

If you are going to use the peek function, however, make sure you understand how it works. In version 0.11, it returns the value _after_ the value that next would return, instead of the same value that next would return.

To get around this, I implemented the following.

#ABSTRACT: Create a closure for iterating over an array
package MyIterator;
use 5.010;
use Moose;
use MooseX::Iterator;
use Carp qw{ croak };

sub generate_peekable_array{

my %opts = @_;

my $aref = $opts{aref}

// croak 'the key "aref" is not optional';

my @array = @{ $aref };

my $num_to_pop = $opts{pop} //= 1;

my $should_peek = $opts{peek} //= 0;

my $num_elements = @array;

if( $num_elements % $num_to_pop != 0){

croak "array of '$num_elements' is not "

. "divisible by 'pop' number of '$num_to_pop'";

}

my $peekable_array = MooseX::Iterator::Array->new(

collection => \@array,

);

return sub {

my @array;

if ($should_peek) {

my $penultimate = $num_to_pop -1;

for ( 1 .. $penultimate ) {

push @array, $peekable_array->next;

}

#peek gives the value that will be available after calling next

my $next_value = $peekable_array->peek;

push @array, $peekable_array->next;

push @array, $next_value;

}

else {

for ( 1 .. $num_to_pop ) {

push @array, $peekable_array->next;

}

}

return @array;

};

}

1;

ORIGINAL REVIEW:
I was just getting ready to create my own iterator for my Moose attribute, when I found this.

Here is working code from my current project using this module:

has 'row_iterator' => (

is => 'ro',

isa => 'MooseX::Iterator::Array',

lazy => 1,

default => sub {

my $self = shift;

MooseX::Iterator::Array->new( collection => $self->{rows} );

},
);

Christopher Bottoms - 2010-08-04T10:09:32 (permalink)

Was this review helpful to you?  Yes No