Perl300 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to use print <<HTML; to print html from perl code. I'll have to use perl code to work some business logic and depending on the result will have to print html. So need to switch between perl code and print html in the same .pl
When I tried to use print <<HTML; to print a bunch of html tags and then stop it for some perl code and resume, I don't get any syntax error but the page when printed shows all the code after the first ending "HTML".
Sample code (from a much larger chunk) is:#!/usr/bin/perl use strict; use warnings; use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser); #print "Content-type: text/html\n\n"; my $database = "dbname"; my $db_server = "hostname"; my $user = "dbusername"; my $password = 'pwd'; #single quote as it contained $ my $query = new CGI; print $query->header; my $dbh = DBI->connect("DBI:mysql:$database:$db_server",$user,$passwor +d); my $statement = "select active from comm_desk_widget_status where user + = 'admin' order by name asc"; my $sth; $sth = $dbh->prepare($statement) or die "Couldn’t prepare the query: $ +sth->errstr"; my $rv = $sth->execute or die "Couldn’t execute query: $dbh->errstr"; print <<HTML; #start HTML printing in chunk <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" +> <title>CommDesk Dashboard</title> <style> body { background-color: AliceBlue; } span.note1 {float:left} span.note2 {font-size:80%} table#t01, #t01 th, #t01 td { border: none; border-collapse: collapse; font-size:80%; } </style> </head> <body> <div style="float:left; width:50%"> <b>Call Volume Tab Configuration</b> <br /><span class="note2">Customize your personal view of the Call Vol +ume Tab.</span> HTML; #stop HTML printing in chunk. But when this file is fetched from + browser, it displays everything starting from the following line unt +il next while (my \@row = $sth->fetchrow_array) { print "<table id="t01">"; print "<tr>"; if (row[0] eq '1') { print "<td><form><input type="checkbox" name="trunk_usage" val +ue="1" checked>% Trunk Usage</form></td>"; } else { print "<td><form><input type="checkbox" name="trunk_usage" val +ue="1">% Trunk Usage</form></td>"; } if (row[1] eq '1') { print "<td><form><input type="checkbox" name="pre_ivr" value=" +1" checked>Pre-IVR Call Volume</form></td>"; } else { print "<td><form><input type="checkbox" name="pre_ivr" value=" +1">Pre-IVR Call Volume</form></td>"; } print "</tr>"; print "<tr>"; if (row[2] eq '1') { print "<td><form><input type="checkbox" name="trunk_group" val +ue="1" checked>Trunk Group Utilization</form></td>"; } else { print "<td><form><input type="checkbox" name="trunk_group" val +ue="1">Trunk Group Utilization</form></td>"; } if (row[3] eq '1') { print "<td><form><input type="checkbox" name="average_speed" v +alue="1" checked>Average Speed of Answer</form></td>"; } else { print "<td><form><input type="checkbox" name="average_speed" v +alue="1">Average Speed of Answer</form></td>"; } print "</tr>"; print "<tr>"; if (row[4] eq '1') { print "<td><form><input type="checkbox" name="outage_call" val +ue="1" checked>Outage Call Volume</form></td>"; } else { print "<td><form><input type="checkbox" name="outage_call" val +ue="1">Outage Call Volume</form></td>"; } if (row[5] eq '1') { print "<td><form><input type="checkbox" name="ivr_call" value= +"1" checked>IVR Call Volume</form></td>"; } else { print "<td><form><input type="checkbox" name="ivr_call" value= +"1">IVR Call Volume</form></td>"; } print "</tr>"; print "<tr>"; if (row[6] eq '1') { print "<td><form><input type="checkbox" name="non_outage_call" + value="1" checked>Non-Outage Call Volume</form></td>"; } else { print "<td><form><input type="checkbox" name="non_outage_call" + value="1">Non-Outage Call Volume</form></td>"; } if (row[7] eq '1') { print "<td><form><input type="checkbox" name="post_ivr" value= +"1" checked>Post-IVR Call Volume</form></td>"; } else { print "<td><form><input type="checkbox" name="post_ivr" value= +"1">Post-IVR Call Volume</form></td>"; } print "</tr>"; print "</table>"; print <<HTML; #start HTML printing in chunk. Browser shows code until +above line on page </div> </body> </html> HTML
UPDATE (so anyone who stumbles across this node will know what were the take aways I was able to use, and probably they should also): After receiving lot of great advice, I was able to use following:
Used hash instead of array
Rearranged code to put the HTML generating code in subs, so the HTML is removed from main program flow.
I wanted to use some template as well but I can only use what I have available on the server, and Template::Tiny is not available
|
|---|