Perl 101 (part 7) - CGI Basics - A Cure For Low Self-Esteem (Page 5 of 6 )
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "<html><body><h1>God, I'm good!</h1></body></html>";
And when you view this page in your browser (assuming you've
placed it in the appropriate place with the appropriate permissions, and that the file is called "good.cgi") by surfing to http://localhost/cgi-bin/good.cgi, you'll see this:
God, I'm good!
Let's dissect this a bit. The first line of the script is one
you'll have to get used to seeing in all your CGI scripts, as it tells the browser how to render the data that follows. In this case, we're telling the browser to render it as HTML text.
Note the two line breaks following the Content-Type statement - forgetting these is one of the most common mistakes Perl newbies make, and it's the cause of a whole slew of error messages.
Our next line is a print() statement which simply outputs some HTML-formatted text - this is what the browser will see.
You can also use variables when generating your page - as the next example demonstrates:
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "<html><body><h1>Tables</h1>";
print "<table border=2>";
for ($x=1; $x<=5; $x++)
{
print "<tr><td>Row $x</td></tr>";
}
print "</table></body></html>";
This article copyright Melonfire 2000. All rights reserved. Next: GETting Your Form To Work >>
More Perl Articles
More By Vikram Vaswani and Harish Kamath, (c) Melonfire