Mike Gossland's Perl Tutorial Course for Windows


Intro to CGI | First Page | Printing HTML | Serving HTML | Serving Edited


Chapter 5. Introduction to CGI

Sending your first page to the browser

If you have a working perl web server, you can verify your setup by sending a simple page from the server to the browser. Here is a simple example of such output:

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 does. (It's a standard MIME header.) If you were just outputting plain text to your browser, then you could use "text/plain" instead of "text/html".

It is very important to send two newlines after the header, i.e., have a single line followed by a 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".