Generating HTML Document with CGI.pm

This section provides a tutorial example on how to generate a HTML document with methods provided in CGI.pm module.

To learn how to use the CGI.pm module, let's start with those methods that creates HTML elements for building the returning HTML document. Here is a simple example Perl CGI script: CGI-pm-HTML-Tags.pl:

#!c:/local/perl/bin/perl.exe
#- CGI-pm-HTML-Tags.pl
#- Copyright (c) 2014, HerongYang.com, All Rights Reserved.
   
   use CGI;
   $query = CGI->new();

   print $query->header();
   print $query->start_html(-title=>'CGI-pm-HTML-Tags.pl');
   print $query->h3('Welcome to HTML tag test page!');
   print $query->p('This is my', $query->strong('first test'), 
      'with CGI.pm module.');
   print $query->hr(); 
   print $query->pre(
      'Hamlet, Act III, Scene I'."\n"
      .'by William Shakespeare'."\n"
      ."\n"
      .'To be, or not to be:'."\n"
      .'that is the question:'."\n"
      .'...'."\n"
      );
   print $query->end_html();

Copy CGI-pm-HTML-Tags.pl to C:\local\apache\htdocs and browse to: http://localhost/CGI-pm-HTML-Tags.pl.

You should see that the HTML document generated by the script is working correct. See the picture below:
HTML Document Generated by CGI.pm

Note that this running this script requires Perl CGI support to be turned on in the htdocs folder.

Last update: 2014.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

CGI.pm Module for Building Web Pages

 What Is CGI.pm?

Generating HTML Document with CGI.pm

 Retrieving Query Parameters and Headers

 Retrieving Environment and Script Information

 Redirecting Browser to a URI

 "Refresh" http-equiv Meta Tag

 What Is CGI::Cookie?

 Retrieving and Setting HTTP Cookies

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version