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 and Object Oriented Perl: Output

May 10, 1999

We've now completed reading the input from the form submission. What to do with it? Anything, really. Our Perl program could process this data in any number of ways, from performing arithmetic calculations to using the data as parameters in a database lookup. Of course, executing these tasks can vary greatly in complexity. Regardless, it is likely that we ultimately want to output some content back to the user's web browser. After all, right now all the poor user sees is the spinning browser logo, anxiously awaiting some sort of result from our Perl program.

process.cgi

#!/usr/bin/perl
use CGI;
#create an instance of the CGI object 
$cgiobject = new CGI;

#grab the values submitted by the user
$name=$cgiobject->param("username");
$age=$cgiobject->param("userage"); 

#output HTML header to web browser
print $cgiobject->header;

#evaluate data, output appropriate message
print "<HTML><BODY>";
print "<CENTER>Hello, $name</CENTER><BR>";
if ($age<18)
 {print "<H3>We\'re sorry, but you are quite young ".
        "to use Currency Central. Money is the root ".
        "of all evil. Please ask a parent.</H3>"}
else
 {print "<H3>Congratulations! You are beyond the age ".
        "of corruption. Click <a href=\"next.html\">here</a>".
        "to continue.</H3>"}

print "</BODY></HTML>";

Picking up this riveting narrative at the point where we output the HTML header to the web browser -- the CGI object possesses a method header, which when called outputs an appropriate set of header text to the web browser so that it properly displays the upcoming output as HTML. We don't need to know anything about the header itself; that's what makes the CGI object a wonderful tool. Simply output the header before you output any HTML and the deed is done.

In the remainder of this Perl program we essentially construct a new web page on-the-fly. Using familiar print functions, we output the necessary HTML to produce the desired page. Notice that we can interweave data from the Perl program into the output page. For instance, the greeting "Hello, $name" will substitute the user's name.

An if...else statement is used to evaluate the user's age; in this case, the only action that is taken is to output a different message depending upon the given age.

There are some additional Perl syntactical tricks strewn in this example which may be worth some notes. Notice the escaped single quote in "We're". This simply ensures that Perl outputs a single quote rather than interpreting it as part of the syntax of our statement. Typically, the escape sequence notation is used when a character might be ambiguous to Perl.

Also note the way the output strings have been broken up in the two long print statements. Although we could have simply typed one long line, it would have been difficult to read within this article. To make the line more legible, the output string has been broken into portions, with a concatenation operator between each portion. The result is exactly the same as if we typed one long line.

To summarize, we've seen the two most common methods of the CGI object: param, which retrieves named pieces of data submitted from the web page, and header, which prepares the web browser for HTML content.

CGI and Object Oriented Perl: Input
The Perl You Need to Know
Form Validation


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