[Petal] [Patch] TAL style repeat object

Fergal Daly fergal at esatclear.ie
Sat Jul 19 22:27:11 BST 2003


Hi all,
	final patch for the day I think and I seem to have hit upon a massive speedup 
for loops. This one adds a TAL style repeat counter variable to Petal. So 
instead of doing

    my @array = @{$hash->get ('b')};
    for (my $i=0; $i < @array; $i++) {
        my $hash = $hash->new();
        my $count= $i + 1;
        $hash->{__count__}    = $count;
        $hash->{__is_first__} = ($count == 1);
        $hash->{__is_last__}  = ($count == @array);
        $hash->{__is_inner__} = (not $hash->{__is_first__} and not 
$hash->{__is_last__});
        $hash->{__even__}     = ($count % 2 == 0);
        $hash->{__odd__}      = not $hash->{__even__};
        $hash->{'a'}        = $array[$i];
        $res .= "<a></a>";
    }

you can get Petal to do

    my @array = @{$hash->get ('b')};
    {
        my $repeat = Petal::Repeat->new($#array);
        local $hash->{repeat} = $repeat;
        for (my $i=0; $i < @array; $i++) {
            $hash->{'a'}        = $array[$i];
            $res .= "<a></a>";
            $repeat->next;
        }
    }

then you can do

repeat/index, repeat/number, repeat/even etc.

To enable the new style of repeat counter, set the variable

$Petal::REPEAT_COUNTER

you can have "petal" for the usual one with __count__, __is_first__ etc.
or "tal" for the new one
or "both" for both, which will let you use both styles but will be slower as 
it has to keep both up to do.

You can do

repeat/number - what element is this
repeat/index - what index in the array is this (1 less than repeat/number)
repeat/even - is number even
repeat/odd - is number odd
repeat/start - is this the start of the array
repeat/end - is this the end of the array

I get a massive speed increase (as in 5 times faster) for my dictionary 
template when using this. I'm a bit shocked about that...

The patch is quite big, it adds an <?endfor?> tag to PI and an _end and 
_endfor method in the code_generator. It has to mess around a little with a 
lot of areas and update the Canonical_Error test.

Hopefully these changes makes it a little easier to do nice error reporting 
and scoped variables,

F
-------------- next part --------------
A non-text attachment was scrubbed...
Name: loop.patch
Type: text/x-diff
Size: 12203 bytes
Desc: not available
Url : http://www.email-lists.org/pipermail/petal/attachments/20030719/32017ca8/loop.bin


More information about the Petal mailing list