in reply to Iterator as a Class or Object Method
An iterator needs to keep some metadata around to know where it is in the data feed. That poses problems for both your ideas. A class method needs lots of internal bookkeeping to distinguish between calls for one instance and another. It will be a poor choice.
An instance method needs bookkeeping to distinguish between one depth of loop and another in nested iteration. Like Perl's each hash iterator, most implementations don't bother, and just live with the restriction to a single loop.
The general solution is to make the iterator an object of its own. Here's an incomplete sketch,
That way you have a Feed instance method which returns a Feed::Iterator object that keeps its own state. The bookkeeping is shoved off onto the perl call stack where it belongs.package Feed::Iterator; sub new { my ($class, $feed) = @_; my $self = _init($feed); bless $self, $class; } sub gimme { # . . . } package Feed; sub iter { my $self = shift; Feed::Iterator->new($self) }
See Limbic~Region's tutorial How To: Make An Iterator for all the fine details.
After Compline,
Zaxo
|
|---|