in reply to Table in Perl CGI
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
|
|---|