PDA

View Full Version : HTML in CGI


cngo
12-14-1998, 08:49 PM
How do you put raw html in a CGI script without the print "<html>stuff</html>\n";

Terra
12-14-1998, 10:31 PM
Hiya cngo,

I usually use 'HERE' docs... Make life much simpler without having to backslash all those silly HTML codes...

2 flavors

with variable interpolation (EOHTML can be anything you want to name it)

my $date = scalar localtime;
print <<EOHTML;
<html>blah blah
yada yada
<body>
<H3>Todays date is: $date
</html>
EOHTML

without variable interpolation, is same as above - the variables just don't replace with contained value...


my $date = scalar localtime;
print <<'EOHTML';
<html>blah blah
yada yada
<body>
<H3>Todays date is: $date
</html>
EOHTML

The EOHTML (or whatever) *must* be flush left with NO leading blanks - or perl will hurl on you...

I use HERE docs all the time -- they are a very big timesaver - and are thusly wise to master from the get go - especially working with stuffing all the HTML fluff inside of them... http://www.aota.net/ubb/wink.gif

OK - today is 2 for 1 day!!! Ever want to interpolate a function return in a HERE doc??? It can be done - as long as it returns in 'list' context! This is my TerraTIP of the day... Plus the syntax just looks so darn clever... http://www.aota.net/ubb/smile.gif

It's a snippet that I wrote for the CNC EMail_MGR program... http://www.aota.net/ubb/wink.gif

print <<EOTABLE3;
</TABLE></TD></TR>

<TR>
<TD COLSPAN="2" ALIGN="center" BGCOLOR="#FFFFFF">
A total of @{[$#mailbox + 2 + $#aliases_tmp + 1]} aliases/mailboxes have been defined
</TD>
</TR>
</TABLE></DIV></CENTER>
EOTABLE3

I almost scrapped the HERE doc way for this section of CGI-HTML, until it dawned on me that I could inject a (list reference - dereferenced within list context to return a scalar value) which makes HERE Docs very happy to comply with your arcane wishes...

I enjoy twisting perl into little knots, resolving all sorts of idiomatic methadology...

Oh well -- I guess in the end that this is FMITYWTK... http://www.aota.net/ubb/frown.gif

--
Andrew Gillespie
Systems Coder
FutureQuest.net

------------------
www.FutureQuest.net (http://www.FutureQuest.net)
--Knowledge spawns creative solutions for complex problems--

cngo
12-15-1998, 03:03 AM
how would I have it create an html file with it?

Terra
12-15-1998, 04:03 AM
You will have to elaborate further...

At this point, I thought I had it covered...

That 'print <<EOHTML' HERE doc *style* does go into the perl script...

Are you slurping in an existing HTML file within a perl script - or are you wanting to generate HTML out of a perl script...

--
Terra
sysAdmin
FutureQuest
--ours is not to reason why, ours is but to do and die--

cngo
12-15-1998, 10:10 PM
What I ment was I wanted to have the script create an .html file in a seperate directroy.

Terra
12-16-1998, 12:22 AM
You have to open a filehandle and write to it...

open FILE, ">foo/bar.html" or die "open failed, $!";

print FILE <<EOHTML;
blah
blah
blah
EOHTML

close FILE;

If you are diving into Perl, I would ***highly*** recommend the O'Reilly series of books... It's a very natural language, with a multitude of idioms... Sometimes it doesn't quite respond to what you are expecting, usually due to what context are you coding for 'Scalar/List/Hash'... Just takes a bit of understanding and Perl will begin to be your best friend...

--
Terra
sysAdmin
FutureQuest

cngo
12-16-1998, 01:07 AM
Thanks. What I was doing was...


open FILE, ">/big/dom/directory/directory ";
print <<EOHTML;

<html junk>

EOHTML
close FILE;

The script still worked for this but didn't print to a file.

Thanks for the Help!