1. Home
  2. Computing & Technology
  3. Perl

Working with CGI.pm In-Depth - Creating an HTML Document

From , former About.com Guide

2 of 5

Titling Your HTML Document

The title of your HTML document is one of the most important attributes for several reasons. First and foremost it establishes and identifies the overall subject of your document. It also has perhaps the highest search engine optimization power of any HTML element - Google, Yahoo and MSN love the document title. Having an Untitled Document can be search engine suicide.

As a reminder, let's look again at our simple program that prints the output on the command line for us to inspect. It's a basic Perl program that includes the CGI.pm and then generates the start of your HTML document using the start_html function:

#!/usr/bin/perl -w

use strict;
use CGI;
my $cgi = new CGI;

print $cgi->start_html;
Let's replace that last line with a new one and see what happens when we add in the -title and -author attributes like so:
print $cgi->start_html(
-title => 'My CGI.pm Web Page'
);
Running this will produce a slightly different output that contains the proper document title:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"><head><title>My CGI.pm Web Page</title>
</head><body>
Much better to pass in the -title attribute and properly title all your HTML documents.
Explore Perl
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Perl
  4. CGI & Web
  5. Working with CGI.pm In-Depth - How to create an HTML document header with CGI.pm

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

All rights reserved.