in reply to How To Use Bootstrap Code in Perl Script?
Whatever else you do (and a HEREDOC is perfectly fine for generating the HTML, as is Template Toolkit), the one thing you have to do differently with CGI is print headers, followed by at least a single blank line.
For example:
#!/usr/bin/perl use strict; use warnings; # CGI headers (note two newlines, one for a blank line) print "Content-type: text/html\n\n"; # Now anything else you do is HTML ... print "<body style='background:cyan'>\n"; print " <center>\n"; print " <h1>Hello world!</h1>\n"; print " </center>\n"; print "</body>\n";
BTW, another style I've gotten in the habit of in place of HEREDOCS is to quote a block of text with qq{ ... }, putting colons ':' at the beginning of each line for visual ease of reading, and finally reformat the text to remove the colons and print it out. For example:
# CGI headers (note two newlines, one for a blank line) print "Content-type: text/html\n\n"; # Another way to format HTML my $text = qq{ :<body style="background:cyan"> : <center> : <h1> Hello world! </1> : </center> :</body> }; output($text); # Subroutines sub output { my ($text) = @_; # Reformat the HTML text (remove leading colons ':') $text =~ s/(^\s+:)|((?<=\n)\s+:)|(\s+$)//g; # And output it print $text; }
|
|---|