##
$sql = "SELECT record_id, attribute_1, attribute_2 FROM theTable";
$sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr);
$sth->execute() or die("Could not execute!" . $dbh->errstr);
while ($record_id, $att_1, $att_2) = $sth->fetchrow_array()) {
$data_hash{$record_id} = {
att_1 => $att_1,
att_2 => $att_2
};
}
$vars = {
data_hash => \%data_hash
};
$template->process($template_file, $vars)
|| die "Template process failed: ", $template->error(), "\n";
####
[% FOREACH record_id IN data_hash.keys.sort.reverse %]
$data_hash.$record_id.att_1 $data_hash.$record_id.att_2
[% END %]