Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


CGI.pm, the Middleman

July 12, 1999

Well, we usually talk about eliminating the middleman when it comes to most of life, but here let's consider welcoming the middleman -- in this case, the CGI.pm Perl module. Of course, we've been using various facets of the CGI module all along. In Part 3 of "The Perl You Need to Know" we used the CGI module to output on-the-fly forms.

In fact, CGI.pm, as it also likes to be called, provides methods for creating HTML output without having to explicitly print HTML code. Whether these methods are advantageous is a valid question, but let's meet them first, and judge them later.

To start with a simple example, suppose we want to start the output of an HTML page. Rather than muck around with <HTML> and <HEAD> tags, we can rely on CGI.pm methods:

#!/usr/local/bin/perl
use CGI;
$cgiobject=new CGI;
$cgiobject->use_named_parameters;
print $cgiobject->header;
#begin HTML page
print $cgiobject->
      start_html(-title=>"Shiny Modern Web Page",
                 -BGCOLOR=>"white");

After creating an instance of the CGI object we call use_named_parameters, which simply makes it easier to name the parameters in creating HTML elements (we'll see in a moment). We supply the CGI method start_html with two named parameters: title and BGCOLOR. Notice the syntax here ... each named parameter begins with a dash and is assigned its value with =>. If you forget the cap > on the equal sign the Perl script will fail, a common source of error.

In sum, the single call to start_html creates output equivalent to the hand-coded HTML:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML><HEAD><TITLE>Shiny Modern Web Page</TITLE>
</HEAD><BODY BGCOLOR="white">

The CGI.pm methods essentially offer a simplified way to express HTML content without handling all of the code. There is a method for most HTML2 and HTML3 tags, which covers most that you'd use. Of course, remembering all these methods is virtually impossible, and so you can consult the CGI.pm HTML Shortcuts Reference for a complete inventory.

We also saw in Part 3 of this series how these methods could be used to create form fields which can otherwise be hairy looking in pure HTML. Consider a series of checkboxes, and compare the HTML output you'd have to code versus the CGI.pm checkbox_group method.

Checkbox group created using CGI.pm checkbox_group method.
print $cgiobject->
    checkbox_group(-name=>'hobbies',
                   -values=>['reading','writing','music',
                             'sports','juggling','pottery',
                             'cooking','fishing','nagging',
                             'cat breeding','stargazing',
                             'navelgazing'],
                   -linebreak=>'false',
                   -columns=>4);

Checkbox group created using pure HTML.
print <<EOF
<TABLE>
<TR><TD>
<INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="reading">reading<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="sports">sports<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="cooking">cooking<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="cat breeding">cat breeding<BR></TD>
</TR>
<TR><TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="writing">writing<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="juggling">juggling<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="fishing">fishing<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="stargazing">stargazing<BR></TD>
</TR><TR><TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="music">music<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="pottery">pottery<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="nagging">nagging<BR></TD>
<TD><INPUT TYPE="checkbox" 
	NAME="hobbies" 
	VALUE="navelgazing">navelgazing<BR></TD>
</TR></TABLE>
EOF

Checkbox group result.
reading sports cooking cat breeding
writing juggling fishing stargazing
music pottery nagging navelgazing

It's clear that in this case, the CGI.pm is a preferable means to generate this HTML content: it's easier to read, edit, debug, and requires less typing. That said, CGI.pm's HTML generation methods are not always this useful. Many HTML tags are simple paired tags, meaning they open and close around a bit of data, such as the <H3> tag for a third-level heading. The coding difference between the CGI.pm and pure HTML approaches is minor:

print $cgiobject->h3("Read all about it.");
vs.
print "<H3>Read all about it</H3>";

Of course, you are free to use CGI.pm to generate complex portions of HTML and stick with pure HTML for the shorter bits. For someone who is quite familiar with HTML, coding simple passages in scratch may be no more or less work than learning the CGI.pm method.

In any case, none of this addresses our outstanding problem with generating on-the-fly Web pages which is that you usually don't need to generate the entire page -- only the part that changes. Enter templates.

Code Like Grandma Used to Make
The Perl You Need to Know
Tantalizing Templates: Live Date


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers