Web Perl   «Prev  Next»

Lesson 9A simple CGI program
ObjectiveWalk through Hello, World in Perl for CGI.

Simple CGI Program in Perl

To get a good feel for how to write a CGI program, let us start with the traditional "Hello, World" program.
This will allow you to concentrate on the mechanics of getting your first CGI program up and working, without worrying about the details of a complicated algorithm.

#!/usr/bin/perl
$CRLF = "\x0d\x0a";
print "Content-Type: text/html$CRLF$CRLF";
print "<H1>Hello, World!</H1>\n";

At the beginning of the above program, you will notice a line like this:
$CRLF = "\x0D\x0A";
This defines the carriage-return/linefeed pair for terminating the MIME header.


Linefeeds in Perl

Actually "\r\n", will work in most systems, but \n may mean something different on systems which use something besides a single linefeed to terminate a line in a normal text file.
In particular, some implementations of Perl on NT and Mac systems may have a non-standard use for \n.
Because it is important to get this right, we use a hexadecimal definition here to avoid these potential conflicts.

Simple CGI - Quiz

Click the Quiz link below to test your knowledge of CGI. Simple CGI - Quiz
In the next lesson, the above program will be run on your own server.
This will prepare for your CGI experiments throughout the rest of this course.