Hello monks,

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


In reply to [Solved]: Can we use print <<HTML; twice in same file? by Perl300

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.