Previous | Top | Next | Search | Comments

CGI programming

HELLO, WORLD!

The simplest of scripts.

It is a tradition to begin any programing demonstration with a "Hello, World!" example; an example that displays a simple text message. That is exactly what our first example does here. Give it a try:

Hello, World!

The script itself is only two lines long and saved as 01-helloWorld.cgi:

#!/usr/local/bin/perl
print "content-type: text/html\n\nHello, World!";

FILENAMES

The first thing to keep in mind is the name of the script, specifically its extention (.cgi). In this case .cgi denotes an executable script to the HTTP server as defined in its MIME types table. If your script is not saved with a defined extenstion, then, by default, the content of the script will be returned to the client and not the result of the script's execution.

WRAPPERS

The first line of the script is a comment. All lines begining with a hash mark (#) are considered comments. On Unix platforms, if the first line of a script is a comment and followed by an exclamation point (! and sometimes called a "bang"), then the text following the exclamation point is the considered to be the application used to interpret the balance of the text. In this case, the application "/usr/local/bin/perl" is called. This convention is called a "wrapper." The Macintosh and Windows platforms also need wrappers and are available from the pages describing their respective Perl ports.

OUTPUT

The second line of the script represents the real guts of the demonstration:

  1. The print statement outputs the content of everything between the quote markes and until the end of the line (;)
  2. The content-type: text/html\n is the "magic line" necessary for your WWW browser to know how to handle the incoming text. ("Remember the HTTP headers the Introduction?").
  3. A single carriage return (\n) delimits the HTTP header from its data just like Internet email (SMTP) messages.
  4. Finally, the text Hello, World! is printed.


Previous | Top | Next | Search | Comments

Version: 1.5
Last updated: 2004/12/23. See the release notes.
Author: Eric Lease Morgan (eric_morgan@infomotions.com)
URL: http://infomotions.com/musings/waves/