| 1 | # | ||
| 2 | # Copyright (C) 1997 Ken MacLeod | ||
| 3 | # Class::Iter is free software; you can redistribute it and/or | ||
| 4 | # modify it under the same terms as Perl itself. | ||
| 5 | # | ||
| 6 | # $Id: Iter.pm,v 1.1.1.1 1997/10/18 16:20:00 ken Exp $ | ||
| 7 | # | ||
| 8 | |||
| 9 | package Class::Iter; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | |||
| 13 | # | ||
| 14 | # Internally, a Class::Iter is an array, | ||
| 15 | # | ||
| 16 | # [0] -- delegate | ||
| 17 | # [1] -- parent iterator | ||
| 18 | # [2] -- parent's array | ||
| 19 | # [3] -- current index | ||
| 20 | # | ||
| 21 | # | ||
| 22 | |||
| 23 | sub parent { | ||
| 24 | # $self->{parent} | ||
| 25 | return $_[0]->[1]; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub is_iter { | ||
| 29 | return 1; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub root { | ||
| 33 | return $_[0] | ||
| 34 | if !defined $_[0]->[1]; | ||
| 35 | |||
| 36 | # $self->{parent}->root; | ||
| 37 | return $_[0]->[1]->root; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub rootpath { | ||
| 41 | if (defined $_[0]->[1]) { | ||
| 42 | return ($_[0]->[1]->rootpath, $_[0]); | ||
| 43 | } else { | ||
| 44 | return ($_[0]); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | # return a new proxy for the next object | ||
| 49 | sub next { | ||
| 50 | my @self = @$_[0]; | ||
| 51 | |||
| 52 | return $_[0] | ||
| 53 | if ($self[3] == $#{$self[2]}); | ||
| 54 | |||
| 55 | shift (@self); | ||
| 56 | return $self[1][$self[2] + 1]->iter (@self); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub at_end { | ||
| 60 | return $_[3] == $#{$_[2]}; | ||
| 61 | } | ||
| 62 | |||
| 63 | # returns what I'm pretending to be | ||
| 64 | sub delegate { | ||
| 65 | return $_[0]->[0]; | ||
| 66 | } | ||
| 67 | |||
| 68 | # `is_same' returns true if the two iterators are pointing to the same | ||
| 69 | # object or if our delegate *is* the object | ||
| 70 | sub is_same { | ||
| 71 | my $self = shift; | ||
| 72 | my $obj = shift; | ||
| 73 | |||
| 74 | return ($self->[0] == $obj | ||
| 75 | || (ref ($self) eq ref ($obj) | ||
| 76 | && $self->[0] == $obj->[0])); | ||
| 77 | } | ||
| 78 | |||
| 79 | package Class::Scalar::Iter; | ||
| 80 | @Class::Scalar::Iter::ISA = qw{Class::Iter}; | ||
| 81 | |||
| 82 | 1; | ||
| 83 | __END__ |