Skip Menu |

This queue is for tickets about the Object-Iterate CPAN distribution.

Report information
The Basics
Id: 24298
Status: stalled
Priority: 0/
Queue: Object-Iterate

People
Owner: bdfoy [...] cpan.org
Requestors: rjbs [...] cpan.org
Cc:
AdminCc:

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



Subject: per-obj names for iterator methods [PATCH] [TESTS]
This patch takes care of your To-do item from POD. I did not update the version number or changelog. -- rjbs
Subject: own-names.patch
diff -Nur Object-Iterate-1.10/MANIFEST Object-Iterate-1.10-rjbs/MANIFEST --- Object-Iterate-1.10/MANIFEST 2007-01-09 23:36:34.000000000 -0500 +++ Object-Iterate-1.10-rjbs/MANIFEST 2007-01-10 08:57:12.000000000 -0500 @@ -14,6 +14,7 @@ t/iterate.t t/load.t t/map.t +t/own_names.t t/pod.t t/pod_coverage.t t/prereq.t diff -Nur Object-Iterate-1.10/lib/Iterate.pm Object-Iterate-1.10-rjbs/lib/Iterate.pm --- Object-Iterate-1.10/lib/Iterate.pm 2007-01-09 23:37:00.000000000 -0500 +++ Object-Iterate-1.10-rjbs/lib/Iterate.pm 2007-01-10 08:54:52.000000000 -0500 @@ -83,16 +83,45 @@ $Init = '__init__'; $Final = '__final__'; +BEGIN { + my %method = ( + next => 'Next', + more => 'More', + init => 'Init', + final => 'Final', + ); + + no strict 'refs'; + for my $which (keys %method) { + *{"_$which\_method"} = sub { + my ($object) = @_; + my $custom_finder = "_object_iterate_$which"; + + my $method = eval { $object->$custom_finder }; + + if (! $method and UNIVERSAL::can( $object, ${$method{$which}} )) { + $method = ${$method{$which}}; + } + + return $method; + }; + } +} + sub _check_object { - croak( "iterate object has no $Next() method" ) - unless UNIVERSAL::can( $_[0], $Next ); - croak( "iterate object has no $More() method" ) - unless UNIVERSAL::can( $_[0], $More ); + my $next = _next_method($_[0]); + my $more = _more_method($_[0]); + + croak( "iterate object has no $Next() method" ) unless $next; + croak( "iterate object has no $More() method" ) unless $more; - $_[0]->$Init if UNIVERSAL::can( $_[0], $Init ); + my $init = _init_method($_[0]); + my $final = _final_method($_[0]); - return 1; + $_[0]->$init if $init; + + return ($next, $more, $init, $final); } =over 4 @@ -119,18 +148,18 @@ my $sub = shift; my $object = shift; - _check_object( $object ); + my ($next, $more, $init, $final) = _check_object( $object ); - while( $object->$More ) + while( $object->$more ) { local $_; - $_ = $object->$Next; + $_ = $object->$next; $sub->(); } - $object->$Final if $object->can( $Final ); + $object->$final if $final; } =item igrep BLOCK, OBJECT @@ -157,20 +186,20 @@ my $sub = shift; my $object = shift; - _check_object( $object ); + my ($next, $more, $init, $final) = _check_object( $object ); my @output = (); - while( $object->$More ) + while( $object->$more ) { local $_; - $_ = $object->$Next; + $_ = $object->$next; push @output, $_ if $sub->(); } - $object->$Final if $object->can( $Final ); + $object->$final if $final; wantarray ? @output : scalar @output; } @@ -199,20 +228,20 @@ my $sub = shift; my $object = shift; - _check_object( $object ); + my ($next, $more, $init, $final) = _check_object( $object ); my @output = (); - while( $object->$More ) + while( $object->$more ) { local $_; - $_ = $object->$Next; + $_ = $object->$next; push @output, $sub->(); } - $object->$Final if $object->can( $Final ); + $object->$final if $final; @output; } @@ -247,10 +276,6 @@ If, for some reason, I disappear from the world, one of the other members of the project can shepherd this module appropriately. -=head1 TO DO - -* let the methods discover the method names per object. - =head1 CREDITS Thanks to Slaven Rezic for adding C<__init__> support diff -Nur Object-Iterate-1.10/t/check_arg.t Object-Iterate-1.10-rjbs/t/check_arg.t --- Object-Iterate-1.10/t/check_arg.t 2002-10-22 14:07:09.000000000 -0400 +++ Object-Iterate-1.10-rjbs/t/check_arg.t 2007-01-10 08:53:42.000000000 -0500 @@ -5,8 +5,9 @@ use Object::Iterate; use Object::Iterate::Tester; -ok( Object::Iterate::_check_object( - Object::Iterate::Tester->new() ), +my @ok = Object::Iterate::_check_object( Object::Iterate::Tester->new() ); + +ok( @ok, 'Tester object can use Object::Iterate' ); my $result = not eval{ Object::Iterate::_check_object( {} ) }; diff -Nur Object-Iterate-1.10/t/own_names.t Object-Iterate-1.10-rjbs/t/own_names.t --- Object-Iterate-1.10/t/own_names.t 1969-12-31 19:00:00.000000000 -0500 +++ Object-Iterate-1.10-rjbs/t/own_names.t 2007-01-10 08:58:49.000000000 -0500 @@ -0,0 +1,29 @@ +# $Id: iterate.t,v 1.3 2002/10/22 23:58:55 comdog Exp $ +use strict; + +use Test::More tests => 2; + +use Object::Iterate qw(iterate); + +my $o = T->new(); +isa_ok( $o, 'T' ); + +my @out = (); +iterate { push @out, "$_$_" } $o; + +my @expected = qw( AA BB CC DD EE FF ); + +ok( eq_array( \@out, \@expected ), 'Iterate returned the right thing' ); + +BEGIN { + package T; + + sub new { bless { A => [ 'A' .. 'F' ] }, __PACKAGE__ } + sub _object_iterate_init { 'I' } + sub _object_iterate_next { 'N' } + sub _object_iterate_more { 'M' } + + sub I { $_[0]{Pos} = 0 } + sub N { $_[0]{A}[ $_[0]{Pos}++ ] } + sub M { $_[0]{Pos} > $#{ $_[0]{A} } ? 0 : 1 } + } diff -Nur Object-Iterate-1.10/t/test_manifest Object-Iterate-1.10-rjbs/t/test_manifest --- Object-Iterate-1.10/t/test_manifest 2005-03-11 23:12:06.000000000 -0500 +++ Object-Iterate-1.10-rjbs/t/test_manifest 2007-01-10 08:58:06.000000000 -0500 @@ -9,5 +9,6 @@ grep.t map.t iterate.t +own_names.t init.t final.t
From: BDFOY [...] cpan.org
On Wed Jan 10 09:02:24 2007, RJBS wrote: Show quoted text
> This patch takes care of your To-do item from POD. > > I did not update the version number or changelog.
Thanks for the patch :) Unfortunately, it breaks the somewhat kludgey interface with $More, $Next, etc. that I had there before, along with the special method names __more__ and __next__. I'll have to look into that to integrate the patch. Also, you didn't update the docs to explain what you did :)
Subject: Re: [rt.cpan.org #24298] per-obj names for iterator methods [PATCH] [TESTS]
Date: Wed, 10 Jan 2007 12:25:56 -0500
To: brian_d_foy via RT <bug-Object-Iterate [...] rt.cpan.org>
From: Ricardo SIGNES <rjbs [...] cpan.org>
* brian_d_foy via RT <bug-Object-Iterate@rt.cpan.org> [2007-01-10T12:19:12] Show quoted text
> Unfortunately, it breaks the somewhat kludgey interface with $More, > $Next, etc. that I had there before, along with the special method > names __more__ and __next__. I'll have to look into that to integrate > the patch.
I'm not sure it does. The logic that looks for the method name to use falls back to $More, etc. It only makes another interface available, which takes precedence. The special method names still work, obviously, or all your tests would have stopped passing. I did not check that you had tests for changing $More, but now I see there are none. I have added one (file attached) and it passes. So: This patch does not break existing functionality. I wouldn't write a patch like that! ;) Show quoted text
> Also, you didn't update the docs to explain what you did :)
Well, that *is* true. -- rjbs

Message body is not shown because sender requested not to inline it.

On Wed Jan 10 12:26:15 2007, RJBS wrote: m not sure it does. The logic that looks for the method name to use Show quoted text
> falls > back to $More, etc. It only makes another interface available, which > takes > precedence. The special method names still work, obviously, or all > your tests > would have stopped passing.
That assumes that I wrote good tests, which I really didn't. I was curious if it still worked, discovered that I hadn't written a test that would check that, then wrote those tests. It broke. Remember, it's not enough to assume that things work because the tests still pass. You need to write a test that shows it explicitly. I'm not guessing that it's broken. I've verified it and am now thinking about how to fix it. It's not a big deal.
From: rjbs [...] cpan.org
On Wed Jan 10 12:30:08 2007, BDFOY wrote: Show quoted text
> That assumes that I wrote good tests, which I really didn't. I was > curious if it still worked, discovered that I hadn't written a test that > would check that, then wrote those tests. > > It broke.
...but I also wrote a test to do this, now attached as pkg_names.t, and it passes. If there is a flaw in that test, please let me know what it is when you've gotten any existing bug fixed. I can't tell.