Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML in perl script

Status
Not open for further replies.

Porshe

Programmer
Jun 5, 2001
38
US
Hi everyone! I need to format a perl report with html. Do I need to use a module and if so, which one? Is it start_html or something like that?

Thanks!
 
CGI.pm more specifically. That module gives you certain methods to print out certain HTML things. BTW, CGI.pm is included in the standard Perl installation, so you don't need to download it .

Here's a brief example from "The Official Guide to Programming with CGI.pm" by Lincoln Stein, the creator of the CGI.pm module:

straight HTML:
-----------
print <<END_HTML;
<HTML><HEAD>
<TITLE>Hi Mom</TITLE>
</HEAD>
<BODY>
<H1>CGI Scripting is Easy with CGI.pm</H1>
<P ALIGN=&quot;CENTER&quot;>So elegant, don't you think?</P>
</BODY>
</HTML>
END_HTML

using CGI.pm instead:
----------------
print start_html('Hi Mom'),
h1('CGI Scripting is Easy with CGI.pm'),
P( {-align=>CENTER}, 'So elegant, don't you think?'),
end_html;


I'll be honest - I prefer to print out the straight HTML myself because I like to know exactly what gets printed. I guess I just never got comfortable enough using CGI.pm. I do use CGI.pm for certain things, like form field handling with the &quot;param&quot; function, and file upload handling, but I stay away from it for general HTML printing - just my preference.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
You can also print the tags. I am assuming you are printing to a text file with the extension .html. You could do the following:

print qq[

<html>
<head>
<body>
.....
.....
</body>
</html>

];

This will save you some time if you are not familar with the CGI module.

Brandon
 
Personally, I HATE using CGI.pm just to output html. It's cumbersome. The easiest way is just to print the html. The main thing is to make sure that you print a content-type header FIRST, to tell the browser that you're returning html. Also, instead of using the qq[..] form above, the more common method is the &quot;here-doc&quot; form, like this:
Code:
print <<END;
Content-type: text/html

<html>
<head>
<title>...</title>
</head>
<body>
...
</body>
</html>
END
Just make sure that your END statement is on a line all by itself and starts in column 1. Also note that there MUST be a blank line after the content-type header. You can use any variables you want in the html, and you can use quotes or apostrpohes without worrying about escaping them. However, you DO need to escape any literal dollar signs, @ signs (like in email addresses), etc.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi again...

when I run the hi mom script from hmerrill I get this error:

Can't find string terminator &quot;END_HTML&quot; anywhere before EOF at look.pl line 1.

Am I missing something.
and I don't need the content-type line because I'm just printing out in a file but I want html to format the file for me, like line centering, bolding, etc...

thankx
 
Make sure that your have a single line containing only the word END_HTML with nothing after it, and it must start in column 1. It's also case sensitive.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Tracy,

I receive the same error as Porshe when I run the himom script. I have made sure there are no spaces after END_HTML and the case is correct and it is on a line by itself. Could it be something buggy with my Perl install (I'm a Perl newbie - only been working with it for a few months and just started cgi).

Thanks,
D.A.
 
Here's another thing that could cause a problem: make sure there is at least a blank line after your END_HTML line. For some reason, if it's the last line in your program it doesn't work right.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top