As a general thing mixing HTML and code is a Bad Thing because it makes it harder to maintain both the code and the HTML. Better is to use modules such as HTML::Template which lets you write most of the page as an ordinary HTML file and add a little magic to allow parts of the HTML to be provided by the script. Consider:

#!/usr/bin/perl use strict; use warnings; use HTML::Template; my $htmlStr = <<HTML; Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE HTML> <html> <head> </head> <body> <table border=1> <TMPL_LOOP name=fruits> <tr> <td><TMPL_VAR name=fruit /></td> <td><TMPL_VAR name=color /></td> </tr> </TMPL_LOOP></table> </body> </html> HTML my %tbl = ( orange => 'orange', lime => 'lime', lemon => 'lemon', bannannanna => 'yellow' ); my @fruits = map {{fruit => $_, color => $tbl{$_}}} keys %tbl; my $template = HTML::Template->new(scalarref => \$htmlStr); $template->param(fruits => \@fruits); print $template->output();

Prints:

Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE HTML> <html> <head> </head> <body> <table border=1> <tr> <td>lime</td> <td>lime</td> </tr> <tr> <td>orange</td> <td>orange</td> </tr> <tr> <td>lemon</td> <td>lemon</td> </tr> <tr> <td>bannannanna</td> <td>yellow</td> </tr> </table> </body> </html>

Usually an external file would be used instead of the here doc and string in the sample.

Update: output fixed - thanks CountZero

Premature optimization is the root of all job security

In reply to Re: Table in Perl CGI by GrandFather
in thread Table in Perl CGI by alokranjan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.