Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Table in Perl CGI

by GrandFather (Saint)
on Aug 02, 2015 at 09:54 UTC ( [id://1137164]=note: print w/replies, xml ) Need Help??


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

Premature optimization is the root of all job security

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1137164]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found