views:

183

answers:

1

I need to create a 2 tables in HTML format. Each has 5 rows:

1st Table

  • 1st row has FRUITS in it, occupying all columns
  • 2nd row has January(month), occupying all columns
  • 3rd row has names of some 6 fruits (apple, orange, grapes,...)These names do not change. so this row has 6 columns
  • 4th row has rates for each fruit ( 10,20,30..) so this has 6 columns.
  • 5th row has corresponding message for each fruit showing as Available or not.

2nd Table

If it is available the background color for the cell should be green and if not RED.

  • 1st row has VEGETABLES in it, occupying all columns
  • 2nd row has February(month), occupying all columns
  • 3rd row has names of some 6 vegetables (tomato, potato..)These names do not change. so this row has 6 columns
  • 4th row has rates for each vegetable ( 10,20,30..) so this has 6 columns.
  • 5th row has corresponding message for each vegetable showing as Available or not.If it is available the background color for the cell should be green and if not RED.

All this data is read from a file having a particular format, it is

<name of fruit/vegetable> price <available or not>

The names of fruits and vegetable do not change , it will be same for both the tables. However, it might be possible that data for a particular fruit/vegetable is not present. if it is not present the the column for that should show N/A with white background.

I cannot use MIME:Lite for this. Need to use print <<ENDHTML;

A: 

There are many methods for generating HTML output with Perl. I prefer HTML::Template:

#!/usr/bin/perl
use warnings; use strict;

use HTML::Template;

my $tmpl = HTML::Template->new(scalarref => \ <<EO_TMPL
<!DOCTYPE HTML>
<html><head><title>Table</title></head>
<body>
<table><thead>
    <tr><TMPL_LOOP TH><th><TMPL_VAR CELL></th></TMPL_LOOP></tr>
</thead>
<tbody><TMPL_LOOP TR><tr>
       <TMPL_LOOP TD><td><TMPL_VAR CELL></td></TMPL_LOOP>
       </tr></TMPL_LOOP>
</tbody></table></body></html>
EO_TMPL
);

$tmpl->param(
    TH => [ map { CELL => $_}, qw( Alice Bob Carla David ) ],
    TR => [
        { TD => [ map { CELL => $_ }, qw( a b c d ) ] },
        { TD => [ map { CELL => $_ }, 1 .. 4 ] },
    ],
);

$tmpl->output(print_to => \*STDOUT);
Sinan Ünür