CGI Programming (Part 4)


© Philip Yuson

Who is this For?
This is for those learning to use the CGI module in Perl.

What you Need to have by now
Skills: Perl Programming, Basic HTML Environment: Webserver setup to run your CGI scripts, CGI Module

Checking if you have the CGI module
On the command line, type:

perl -Mcgi If the CPAN module is installed, there should be no errors. If you see an error message, your CGI module is probably not installed. You can install the CGI module through the CPAN shell or using PPM for ActivePerl or DPM for IndigoPerl.

What does the CGI module do? The CGI module allows you to generate HTML pages without really coding the HTML statement. You do this by calling CGI functions. I find this convenient because you do not need to fully know the HTML syntax.

Aside from this, the CGI module performs housekeeping functions internally so you do not need to worry about things like cleaning up the QUERY_STRING or input string, printing out the header and termination records, etc.

Let us start
To use the CGI module, you have to use it within your script

use CGI;
Once you have used the CGI module, you will have to create an instance of it. my $c = new CGI;
We have learned that the first statement from your CGI script should be the Content-type record. Without the CGI module, you will have to manually type this. With the CGI module, you can just do this: $c->header;
You can also specify other parameters depending on the Content-type that you want to generate.

If you do not use the CGI module, you will have to do housekeeping stuff for your input ($ENV{QUERY_STRING} and from <STDIN>). With the CGI module, this is done automatically for you. In fact, the parameters passed to your script are broken up automatically for you. You access the parameters using the param function. To find the parameters passed to your script, you call the param function without any parameter. This returns a list of parameters passed to your script. To find the value of a given parameter, you call the param function with the name of the parameter.

@parmNames = $c->param(); # get all parameter names
$value = $c->param('NAME'); # get the value of the NAME parameter.

Generating other Tags
Generally speaking, you generate the tags by calling the function with the same tag name. To print a BOLD tag, you can do this:

$c->b('This line is in bold');
$c->i('This is in italics');

The result is:
<B>This line is in bold</B>
<I>This is in italics</I>

Go To Page: 1 2


The copyright of the article CGI Programming (Part 4) in Perl is owned by . Permission to republish CGI Programming (Part 4) in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo