Skip Menu |
 

This queue is for tickets about the Iterator-Simple CPAN distribution.

Report information
The Basics
Id: 72629
Status: open
Priority: 0/
Queue: Iterator-Simple

People
Owner: Nobody in particular
Requestors: PCMANTZ [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)

Attachments
0001-add-getline-method-to-iterators-to-emulate-file-hand.patch



Subject: add getline method to iterators
MIME-Version: 1.0
X-Mailer: MIME-tools 5.427 (Entity 5.427)
X-RT-Original-Encoding: utf-8
Content-Type: multipart/mixed; boundary="----------=_1321988095-6895-88"
Content-Length: 0
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: binary
Content-Length: 341
Download (untitled) / with headers
text/plain 341b
I'd really like to be able to pass iterators back to a PSGI server. wrapping up a DBIC resultset in an ifilter would be a great way to stream data. PSGI relies on the getline method to stream data, which has the same effect as the diamond operator ('<>'). This patch adds the getline method to further duck type the file handle interface.
Subject: 0001-add-getline-method-to-iterators-to-emulate-file-hand.patch
MIME-Version: 1.0
Content-Type: text/x-patch; name="0001-add-getline-method-to-iterators-to-emulate-file-hand.patch"
X-Mailer: MIME-tools 5.427 (Entity 5.427)
Content-Disposition: inline; filename="0001-add-getline-method-to-iterators-to-emulate-file-hand.patch"
Content-Transfer-Encoding: binary
Content-Length: 3948
From 2369a8a4bcfd8446ee138c7405a768b0550553dd Mon Sep 17 00:00:00 2001 From: Paul C. Mantz <pcmantz@gmail.com> Date: Tue, 22 Nov 2011 12:42:08 -0600 Subject: [PATCH] add getline method to iterators to emulate file handles better --- lib/Iterator/Simple.pm | 24 +++++++++++++++--------- t/10_basic.t | 20 +++++++++++--------- t/12_list.t | 16 +++++++++++++++- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/lib/Iterator/Simple.pm b/lib/Iterator/Simple.pm index add6406..a07081d 100644 --- a/lib/Iterator/Simple.pm +++ b/lib/Iterator/Simple.pm @@ -43,9 +43,11 @@ sub iter { if($method = $self->can('__iter__')) { return $method->($self); } - if($method = overload::Method($self, '<>') || $self->can('next')) { - return ITERATOR_CLASS->new(sub { $method->($self) }); - } + if ( $method = overload::Method($self, '<>') + || $self->can('next') + || $self->can('getline')) { + return ITERATOR_CLASS->new(sub { $method->($self) }); + } if($method = overload::Method($self, '&{}')) { return ITERATOR_CLASS->new($method->($self)); } @@ -79,6 +81,7 @@ sub is_iterable { $self->isa(ITERATOR_CLASS) or $self->can('__iter__') or $self->can('next') + or $self->can('getline') or overload::Method($self, '<>') or overload::Method($self, '&{}') or overload::Method($self,'@{}') @@ -132,8 +135,10 @@ sub list { push @list, $val while defined($val = $iter->()); return \@list; } - if($method = overload::Method($self, '<>') || $self->can('next')) { - my(@list, $val); + if ( $method = overload::Method($self, '<>') + || $self->can('next') + || $self->can('getline')) { + my (@list, $val); push @list, $val while defined($val = $method->($self)); return \@list; } @@ -366,8 +371,8 @@ sub iarray { use Carp; use overload ( - '<>' => 'next', - '|' => 'filter', + '<>' => 'getline', + '|' => 'filter', fallback => 1, ); @@ -378,7 +383,8 @@ sub iarray { bless $_[1], $_[0]; } - sub next { goto shift } + sub next { goto shift } + *getline = \&next; sub __iter__ { $_[0] } @@ -387,7 +393,7 @@ sub iarray { *chain = \&Iterator::Simple::ichain; *zip = \&Iterator::Simple::izip; *enumerate = \&Iterator::Simple::ienumerate; - *slice = \&Iterator::Simple::islice; + *slice = \&Iterator::Simple::islice; sub head { Iterator::Simple::ihead($_[1], $_[0]); } sub skip { Iterator::Simple::iskip($_[1], $_[0]); } } diff --git a/t/10_basic.t b/t/10_basic.t index 455ba0d..a7f7306 100644 --- a/t/10_basic.t +++ b/t/10_basic.t @@ -1,4 +1,4 @@ -use Test::More tests => 5; +use Test::More tests => 6; use strict; use warnings; @@ -46,6 +46,16 @@ ok( mk_itr(1,5), 'iterator creation' ); #5 { + $itr = mk_itr(0,4); + my @res; + while(defined(my $r = $itr->getline)) { + push @res, $r; + } + is_deeply ( \@res, [0,1,2,3,4], 'getline' ); +} + +#6 +{ $itr = mk_itr(1,5); my @res; while(<$itr>) { @@ -53,11 +63,3 @@ ok( mk_itr(1,5), 'iterator creation' ); } is_deeply ( \@res, [1,2,3,4,5], '"<>" overload' ); } - -# @{} overloads could cause confusion -##6 -#{ -# $itr = mk_itr(12,17); -# is_deeply ( [@{$itr}], [12,13,14,15,16,17], '"@{}" overload' ); -#} - diff --git a/t/12_list.t b/t/12_list.t index 796fb60..dc5e1e1 100644 --- a/t/12_list.t +++ b/t/12_list.t @@ -1,4 +1,4 @@ -use Test::More tests => 8; +use Test::More tests => 9; use strict; use warnings; @@ -44,6 +44,11 @@ use Iterator::Simple qw(list iterator); } { + my $buz = Buz->new(); + is_deeply list($buz) , [0,1,2,3,4,5,6,7,8,9,10,11], 'list(getline implemented)'; +} + +{ is_deeply list(), [], 'list()'; } @@ -91,6 +96,15 @@ use Iterator::Simple qw(list iterator); } } +{ + package Buz; + sub new { bless {i => 0}, $_[0]; } + sub getline { + my($self) = @_; + return if $self->{i} > 11; + return $self->{i}++; + } +} __END__ foo -- 1.7.4.1
MIME-Version: 1.0
X-Mailer: MIME-tools 5.427 (Entity 5.427)
Content-Disposition: inline
Content-Type: text/plain; charset="UTF-8"
Message-ID: <rt-3.8.HEAD-13053-1363560805-402.72629-0-0 [...] rt.cpan.org>
Content-Transfer-Encoding: binary
X-RT-Original-Encoding: utf-8
Content-Length: 302
Download (untitled) / with headers
text/plain 302b
I like this suggestion, but I want to give Rintaro a little more time to respond before going hog-wild on changes. Maybe a month from now, I'll release a v0.07 with this rolled in if we haven't heard from him. (In v0.06, I just wanted to get Iterator::Simple passing tests again with newer Perls.)


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.