Mike Gossland’s Perl Tutorial Course for Windows

Chapter 5. Introduction to CGI

Introduction to CGI
First Page to Browser
Printing HTML
Serving HTML Files
Serving Edited Files

Sending your first page to the browser

During the setup of Perl and the Web Server, you were asked to verify your setup by sending a simple page to the browser. Let's look at it in detail again here:

print "Content-Type: text/html\n\n";

print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>Hello World</TITLE>\n";
print "</HEAD>\n";
print "<BODY>\n";
print "<H4>Hello World</H4>\n";
print "<P>\n";
print "Your IP Address is $ENV{REMOTE_ADDR}.\n";
print "<P>";
print "<H5>Have a nice day</H5>\n";
print "</BODY>\n";
print "</HTML>\n"; 

First of all, it should be clear that this is Perl program does nothing but print HTML text to STDOUT. That's pretty much all there is to sending data out through CGI!

The output does have to follow certain rules through. You can't just print anything you want and expect it to be interpreted by the browser correctly. The first thing that your program must do is to tell the browser what kind of information follows.

Since we are sending HTML output, we must inform the browser that the content is text in HTML format. That's what the first line is for. (It's a standard MIME header.) If you were just outputting plain text to your browser, then you could put "text/plain" instead of "text/html".

It is very important to send two newlines after the header, i.e., a single blank line, otherwise the browser will complain of bad header information.

After the content type header, there is just a complete section of HTML. The only odd bit in the script is the line:

print "Your IP Address is $ENV{REMOTE_ADDR}.\n";

As you have seen, this prints your numeric IP address in the browser window. But what is $ENV{REMOTE_ADDR}? The answer will become clear later. For now just accept that it is part of the data delivered to the input of CGI. How you get at data will come later.

Let's look at some other examples of content and other techniques of creating content "on-the-fly".

Next Page

Search this site or the web:

just this site the whole web

Questions? Feedback?

Email me your comments!