cgi_script

Writing a CGI script

In this tutorial you will learn how to write a perl CGI script.

Firstly you need to ensure that you have access to a web server (that has perl installed), that it has cgi enabled, and that you can put your perl script into the cgi directory.

Starting your script

The CGI module is the easiest way to create a cgi script. While you can do all the parsing and handling headers and encoding urls yourself, why reinvent the wheel.

By using

we are asking that all fatal errors be displayed on the browser, rather than silently written to the web server error log. This makes testing and bug fixing much easier.

We then create a CGI object:

By convention this is nearly always $q or $query.

Starting the output

We use the CGI object to print out the HTTP header for the output:

This gives you the following HTTP header:

If you need to have any extra options in your header, for example you may not want the default ‘type’, you can just pass them in to the header method:

This gives you the following HTTP header:

Object Oriented or not

You can either use the CGI object to output HTML in an object oriented fashion, or use the CGI module’s functional interface, or you can print the HTML directly to standard out.

For example, the following examples do exactly the same things:

Object oriented style:

Functional style:

Neither:

In this tutorial we’ll be using the Object Oriented interface to the CGI module to output our HTML.

You can use the CGI module to print any type of HTML tag that exists. One thing to beware of is the capitalization on some functions. This is due to built in Perl functions of the same name:

When you use these functions, remember to capitalize the first letter.

The HTML

To start the HTML, you can use the start_html tag.

If you want to give the page a title, you can use:

Or if you want to pass in more parameters:

When you want to end the html, just use the end_html method:

Tags

You can use the CGI module to print any HTML tag:

Heading tags:

Paragraph tags:

Table tags (there’s more than one way to print a table):

Notice that we’ve passed an anonymous array to the th and td methods. Some CGI methods will accept either scalar variables or an array reference. When the method is passed an array reference, tags will be generated for each element in the array. For example, the following line:

Produces the following output:

Another way of printing tables:

This time we’ve passed scalar values to the th method. As you can see it takes up quite a bit more room. But the HTML generated would be exactly the same as the previous table example (except for the border and cellpadding in the table tag).

Lists:

Like table cells, you can pass a scalar or an array reference to the li method.

There are more tags than this, but we’re not going to list them all here.

Working example

Below is a full working example of a html page output entirely using the CGI module’s Object Oriented interface.

The output of this program can be seen here.

See Also

Scroll to Top