tags:

views:

83

answers:

1

How do I create an HTML table in Perl?

+5  A: 

Three options.

1/ Print the raw tags in the output of your program (as chao has already shown you). But I don't really recommend that.

2/ Use the HTML shortcut functions from CGI.pm. But I don't think anyone still does that, do they?

3/ Generate your output using a templating system. Something like this, perhaps:

#!/usr/bin/perl

use strict;
use warnings;
use Template;

my @headers = qw(col1 col2);
my @table = (
  ['val11', 'val12'],
  ['val12', 'val22'],
);

my $t = Template->new;

$t->process(\*DATA, { head => \@headers, table => \@table })
  or die $t->error;

__END__
<table>
<tr>[% FOR h IN head %]<th>[% h %]</th>[% END %]</tr>
[% FOR tr IN table -%]
<tr>[% FOR td IN tr %]<td>[% td %]</td>[% END %]</tr>
[% END -%]
</table>
davorg