I love formats, but I wouldn't use it for this. The things formats do and are good at don't come into play here.
Simple print
statements will do. splice
is an under-appreciated built-in that can remove several elements from an array in one go:
use HTML::Entities qw(encode_entities);
my @values = (
'a' .. 'd',
'cats & dogs', '</div>', '"quotes"',
'<script src="foo.js"/>',
'e' .. 'f'
);
my $elements_per_row = 3;
print "<table>\n";
while( my @row = splice @values, 0, $elements_per_row, () ) {
print
'<tr>',
map(
{ '<td>' . encode_entities( $row[$_] ) . '</td>' }
0 .. $elements_per_row - 1
),
'</tr>', "\n"
}
print "</table>\n";
But, you should think about separating your HTML from your program using a templating system of some sort. There are several available for Perl.