Hello, world!
This section demonstrates a simple CGI "Hello, world!" script using Perl.
Programming Issues
All this script has to do is to create an HTML document with "Hello, World!" in the title and in the main heading, and also in a paragraph for good measure.
However, whilst it is almost as simple as that, there is a little more. Each CGI script must also output a
HTTP header, and this must be the first thing
it outputs. Generally, the only thing the CGI script has to include in the HTTP header is the content type, which
will almost always be text/html
. A blank line must be output after the HTTP header, before
the output of the HTML document itself is output, otherwise the server will not know where the header ends, and
where the document begins.
The output will therefore look like:
Content-Type: text/html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title> … (rest of HTML document follows) …
</html>
Usage
Just run the CGI script by requesting it with the link: http://www.paulgriffiths.net/cgi-bin/helloworld.pl.
Source and Downloads
Two versions of the source code are presented, one using simple print
statements, and the second
using Perl's "here document" concept, which is much easier both to code and to follow.