next up previous
Next: 0.3 Data Processing Up: CGI-Perl, GnuPlot, etc - Previous: 0.1 Introduction

0.2 Basic CGI Scripting

In this page I will try to explain the basic elements of a CGI script. Please note that this bit of code uses the Object Oriented way of doing CGI scripting; If you prefer a Functional way, please look at the next subsection - it explains how you can port this code to have a functional structure. The core remains the same, there'll be just a few syntactical changes. If you are not concerned about this, forget it!

To see how this script works, create a file using your favorite editor (pico/vi/emacs/cat/whatever you like!) called ``'' and copy n paste the code shown below into it. I've shown how you can do this using `cat' here. Haifeng's introduction to CGI (Link off the class home page Lab Section [1]) should get you started nicely on the real basics.

[L529guest@biokdd agopu]\$ cat >
## This Code from (slightly modified)

use CGI;

$query = new CGI;

print $query->header;
print $query->start_html("L529 - Example Form");
print "<h1> L529 - Example Form</h1>\n";
print $query->end_html;

sub print_prompt {
    my($query) = @_;

    print $query->start_form;
    print "<em>What's your name?</em><br>";
    print $query->textfield('name');
    print $query->checkbox('Not my real name');

    print "<p><em>Where can you find English Sparrows?</em><br>";
    print $query->checkbox_group(
				 -name=>'Sparrow locations',

    print "<p><em>How far can they fly?</em><br>",
			-name=>'how far',
			-values=>['10 ft','1 mile','10 miles','real far'],
			-default=>'1 mile');

    print "<p><em>What's your favorite color?</em>  ";
    print $query->popup_menu(-name=>'Color',

    print $query->hidden('Reference','Monty Python and the Holy Grail');

    print "<p><em>What have you got there?</em><br>";
    print $query->scrolling_list(
				 -values=>['A Coconut','A Grail','An Icon',
					   'A Sword','A Ticket'],

    print "<p><em>Any parting comments?</em><br>";
    print $query->textarea(-name=>'Comments',

    print "<p>",$query->reset;
    print $query->submit('Action','Shout');
    print $query->submit('Action','Scream');
    print $query->endform;
    print "<hr>\n";

sub do_work {
    my($query) = @_;

    print "<h2>Here are the current settings in this form</h2>";

    foreach $key ($query->param) {
	print "<strong>$key</strong> -> ";
	@values = $query->param($key);
	print join(", ",@values),"<br>\n";

sub print_tail {
    print "<hr>";

Press ``Ctrl + d'' to exit cat.

Make the perl script executable - change permissions:

[L529guest@biokdd agopu]\$ ls -l
total 4
-rw-rw-r--    1 L529guest L529guest     2056 Oct 26 17:44
[L529guest@biokdd agopu]\$ chmod 755 *.pl

Alright now let us see how this code works. I will try to go sequentially down the code and explain some of it.

Try opening the page you just created on your favorite browser. Going to:

...should get you started. Replace `agopu' with your user name or whatever you named your directory to be. Remove `L529guest' if you are using your own account.

next up previous
Next: 0.3 Data Processing Up: CGI-Perl, GnuPlot, etc - Previous: 0.1 Introduction
Arvind Gopu 2006-03-24