1. Home
  2. Computing & Technology
  3. Perl

Working with CGI.pm In-Depth - Creating HTTP Headers

From About.com

1 of 5

Overview

Perl's CGI module, CGI.pm, is a quick and easy way to create CGI applications with embedded HTML. It saves you the hassle of editing and working with actual HTML in your perl programs, or dealing with the overhead of a templating system. In our first in-depth look at the CGI.pm, we're going to be exploring the header and redirect functions.

The header and redirect functions of the CGI.pm are used to easily generate HTTP headers for your documents. Every document that is passed via the HTTP protocol is required to contain a header that identifies the content type, and a handful of important basic information. For these examples, we will just be printing headers to the command line of our system, so there's no need for an actual web browser.

First off, lets look at the default output of CGI.pm's header function:

#!/usr/bin/perl -w

use strict;
use CGI qw/:standard/;
my $cgi = new CGI;

print $cgi->header;
Running this program will generate the following output:
Content-Type: text/html; charset=ISO-8859-1
As you can see, it's a simple Content-Type statement followed by 2 newlines. The default output of the header function assumes a content-type of HTML, with the ISO-8859-1 standard.

So far, nothing impressive, but keep reading as we explore some of the parameters you can pass into this function, starting with changing the content-type.

Explore Perl
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. CGI & Web
  5. Working with CGI.pm In-Depth - How to create HTTP Headers, Content, Attachments and Redirect with CGI.pm

©2009 About.com, a part of The New York Times Company.

All rights reserved.