The comments in code for Template::Iterator::get_all indicate it
should work without needing to call get_first first. It's a little
cumbersome to create an iterator, call get_first, then call get_all,
and then combine the values.
So here's a small patch so that get_all may be called without calling
get_first first.
Regards,
Jonathon Padfield
Index: t/iterator.t
===================================================================
--- t/iterator.t (revision 1180)
+++ t/iterator.t (working copy)
@@ -56,7 +56,31 @@
$rest = $i1->get_all();
ok( scalar @$rest == 5 );
+# get_all with a few values in the iterator
+my $i2 = Template::Iterator->new($data);
+($rest, $err) = $i2->get_all();
+is( scalar @$rest, 7 );
+ok( ! $err);
+($val, $err) = $i2->get_all();
+ok( ! $val);
+is( $err, Template::Constants::STATUS_DONE );
+# get_all with a single value.
+my $i3 = Template::Iterator->new(['foo']);
+($rest, $err) = $i3->get_all();
+is( scalar @$rest, 1 );
+is( pop @$rest, 'foo' );
+ok( ! $err);
+($val, $err) = $i3->get_all();
+ok( ! $val);
+is( $err, Template::Constants::STATUS_DONE );
+
+# get_all with an empty array
+my $i4 = Template::Iterator->new([]);
+($val, $err) = $i4->get_all();
+ok( ! $val);
+is( $err, Template::Constants::STATUS_DONE );
+
test_expect(\*DATA, { POST_CHOMP => 1 }, $vars);
__DATA__
Index: lib/Template/Iterator.pm
===================================================================
--- lib/Template/Iterator.pm (revision 1180)
+++ lib/Template/Iterator.pm (working copy)
@@ -152,10 +152,30 @@
my ($max, $index) = @$self{ qw( MAX INDEX ) };
my @data;
+ # handle cases where get_first() has yet to be called.
+ unless (defined $index) {
+ my ($first, $status) = $self->get_first;
+
+ # refresh $max and $index, after get_first updates MAX and INDEX
+ ($max, $index) = @$self{ qw( MAX INDEX ) };
+
+ # empty lists are handled here.
+ if ($status && $status == Template::Constants::STATUS_DONE) {
+ return (undef, Template::Constants::STATUS_DONE); ## RETURN ##
+ }
+
+ push @data, $first;
+
+ ## if there's nothing left in the iterator, return the single value.
+ unless ($index < $max) {
+ return \...@data;
+ }
+ }
+
# if there's still some data to go...
if ($index < $max) {
$index++;
- @data = @{ $self->{ _DATASET } } [ $index..$max ];
+ push @data, @{ $self->{ _DATASET } } [ $index..$max ];
# update counters and flags
@$self{ qw( INDEX COUNT FIRST LAST ) }
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates