Home > Perl > Web programming > CGI Programming - Part 4
CGI Programming - Part 4
Written by Philip L Yuson   
Who is this For?
This is for those learning to use the CGI module in Perl.


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 &ltSTDIN>). 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>


or displayed as:

This line is in bold
This is in italics

The parameters for the tag are passed as a hash (key-value) pair. If you want to display a text using an arial font with the size of 2, you can do this manually:

<FONT FACE=Arial, SIZE=2>Sample Text</FONT>

or
print $c->font({-face=>'Arial', -size=>'2'}, Sample Text);

To print the text in the same font but in italics and bold, you can do this:
print $c->i($c->b(
$c->font({-face=>'Arial', -size=>'2'}, Sample text)
) );

Generating Tables
To generate a table, you can do this:

print $c->table({-border=>1},
$c->Tr({-valign=>'top'}, [$c->td(['Row0 Col1', 'Col2', 'Col3'])
] ), # Matching close brackets for Tr
$c->Tr({-valign=>'top'},
[$c->td(['Row1 Col1', 'Col2', 'Col3'])
] ), # Matching close brackets for Tr
); # Closing parenthesis for table


The result for this is:

Row 0 Col 1

Col 2

Col 3

Row 1 Col 1

Col 2

Col 3



For more information, you can check out the Perl documentation for CGI module



 
Copyright: © 2017 Philip Yuson