[Petal] Does Petal support a simple while loop?

Fergal Daly fergal at esatclear.ie
Sun Jun 22 22:18:28 BST 2003


I absolutely agree with you on lazy evaluation, I wish Perl were full of 
iterators. The difficulty is how to bring them into Petal.

I agree that the 1-method iterators are nicer but unfortunately there are 
modules out there that use the 2-method version. Most examples of iterators 
you'll find are for other languages and so they must use getNext and hasNext. 
Perl programmers see these examples, see that iterators are cool, implement 
them with 2 methods and don't realise that in Perl they can be even cooler.

This means that we should probably support both styles but see bleow.

> I had thought that the approach I'm suggesting, would
> require no extra syntax beyond allowing petal:repeat's second
> argument to be a method name.  As I type this, I realise that
> it probably already can be a method name but expects that the
> method would return a list of hashes.  If that is how it
> behaves then my proposal probably couldn't be integrated
> easily.  Bother.

Exactly. If you're going to try it, you could start by adding a 3-parameter 
tag called petal:iterate, making it totally separate from petal:repeat. 
You'll have to make it

petal:iterate="item getNext path/to/object"

because if you put the path before the method name you'll have to have some 
way of figuring out where the path ends, which is basically impossible 
without parsing it fully.

If there's great demand for it, we could also have

petal:awkward_java_style_iterator="item hasNext getNext path/to/object"

or people could hide the ugliness with a class like

package Perlify::Iterator;

sub new
{
	my $pkg = shift;
	my $it = shift;
	my $self = bless $it, $pkg;
}

sub getNext
{
	my $self = shift;

	return $self->hasNext ? ($self->getNext) : ();
}

F



More information about the Petal mailing list