use CGI::Ex::Template;
my @a = qw(one two three four five six seven eight);
my $temp = q{
[%~ cols = 3 %]
[%~ rows = a.size DIV cols %]
[%~ IF a.size % cols ; rows = rows + 1 ; END -%]
[%- FOR i IN [1 .. rows] %]
[%- FOR j IN [1 .. cols] %]
[%~ k = (i - 1) * cols + (j - 1) %]
[% a.$k %]
[%- END %]
[%- END %]
};
CGI::Ex::Template->new->process(\$temp, {a => \@a}) || die $CGI::Ex::Template::error;