Web Programming I: Forms and CGI Scripts

UO Library IT Curriculum, JQ Johnson

Exercise 1

Create this simple Perl script. Call it printenv.cgi, and give it a protection of 755. Try it.

#!/usr/bin/perl 
print "Content-type: text/plain\n\n";        
foreach $a (keys %ENV) {
    print "$a = $ENV{$a}\n";
}

Exercise 2

Write an HTML page with a form that implements a search engine for your web site. See sample solution at right (replace user with your user name).

<form method="GET" 
action="http://search.uoregon.edu/cgi-bin/query">
Search my site for:  <input name="q" size="20"
value="                          +url:~user">
<input type="hidden" name="pg" value="q"> 
<p><input type="submit" value="Search"> or 
<input type="reset">  </form>

Exercise 3

Write an HTML form that requests input and sends it as e-mail to yourself. Implement a user interface similar to the example at the right.

Number Preference Survey:
From (email):
Real name:
Favorite number:   or

For further information

Most books on basic HTML have a section on forms. More specialized books cover CGI scripting and Perl. For a different list of recommended books, see <http://www.calendarplus.com/amazon/allbooks.shtml>, or search Amazon.com for keyword "cgi". For Perl books, see also <http://www.perl.org/books/recommendations.html>. Here are a few typical titles:

  • Perl and Cgi for the World Wide Web, by Elizabeth Castro (Addison-Wesley, 1998) [intro.]
  • Learning Perl, 2nd ed, Randall Schwarz et al. (O'Reilly, 1997). [intro., but not beginner]
  • CGI Programming with Perl, 2nd ed, Shishir Gundavaram (O'Reilly, 1999) [intermediate]
  • Teach Yourself CGI Programming in a Week, 3rd Ed., Rafe Coburn and Chris Menon (Sams, 1998) [intermediate]