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 ``simple_complete.pl'' 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 > simple_complete.pl
#!/usr/bin/perl
## This Code from http://www.perldoc.com/perl5.8.0/lib/CGI.html (slightly modified)

use CGI;

$query = new CGI;

print $query->header;
print $query->start_html("L529 - Example CGI.pm Form");
print "<h1> L529 - Example CGI.pm Form</h1>\n";
&print_prompt($query);
&do_work($query);
&print_tail;
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',
				 -values=>[England,France,Spain,Asia,Hoboken],
				 -linebreak=>'yes',
				 -defaults=>[England,Asia]);

    print "<p><em>How far can they fly?</em><br>",
    $query->radio_group(
			-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',
			     -values=>['black','brown','red','yellow'],
			     -default=>'red');

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

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

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

    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) = @_;
    my(@values,$key);

    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 simple_complete.pl
[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:

http://biokdd.informatics.indiana.edu/cgi-bin/L529guest/agopu/simple_complete.pl

...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