Prev Up Next
Go backward to 4.3 Individual Server Side Include Topic
Go up to 4 Major Topics
Go forward to 4.5 Form Processing

4.4 CGI Introduction to Perl Topic #2

The purpose of this section is to continue the introduction of Perl programming. This topic will also lead to something useful for CGI programming. It will help with debugging.

One of the major problems in getting CGI programs to run is the detail that they run in a different environment than when debugging the program. The idea of this lab is to make the environments as much a like as possible.

It is also the plan of this lab to get you to write more Perl programs, so, even if you find something quite like this lab on the Internet, I would like your group to write most of the code anyway. But -- please look at what is available and if you find something useful, do look at the code and see how it was done.

4.4.1 Preparation

  1. Look at the environment as provided to your program when run at the command line. To do this use the env command.
  2. Look at the environment as provided to your program when run as a CGI program. To do this use the program at env.pl What are the differences?
    #!/bin/sh
    echo "Content-type: text/plain"
    echo ""
    echo ""
    env
    
  3. Find out how CGI programs use the standard input.
  4. Read up on subroutines and passing arguments in Perl. See Learning Perl by Schwartz, & Christansen Chapter 8, "Functions", Pages 92 - 100. Also see Programming Perl by Wall, Christansen, & Schwartz Chapter 2, "The Gory Details" (not all of it) pages 111 - 121.
  5. Look at details of (and you are welcome to use) subroutine parse_form_data CGI Programming by Gundavaram On the web at O'Reilly pages 64-67.
  6. You may want to look at reading and writing files again.
  7. Write a little "test" programs for accessing values in the environment.

4.4.2 Group Task

Write a Perl subprogram that can save and restore passed parameters to a cgi program. Modify common.pl or write your own subprogram that usually inputs, to a common format, the data passed to a cgi program with the following additions.
  1. If the program is run from the shell, it reads in the values previously stored in the file saved-arguments. A program can look at the environment t variables to determine if it is being run in the shell. (Environment variable SHELL would be a good choice.)
  2. If the subroutine is called from a program running because it was invoked by a web server, it will process the data normally, but prior to returning, it will write out to a file (saved-arguments) all of the passed parameters.
  3. If the file saved-arguments does not exist, the program invoked by httpd will behave as common.pl did originally.
  4. Also provide a conditional print ability, so that if using the saved environment, additional debug printouts can be included. This can be just a global variable or a subroutine, your choice.
The purpose of this program is to simplify debugging. It should allow you to execute most programs as shell programs with the same environment "functionally" as when the program is run by the Hyper Text Transport Protocol Daemon (HTTPD).

4.4.3 Report

Turn in your group's report (only one copy please) marked as follows:
  1. A printout of your Perl subroutine file.
  2. A printout of a saved environment.
  3. Suggestions on lab.

Instructor: ltaber@pima.edu** My new Home on phRed** The Pima College Site** The Mad Dr. G.'s home page on phred.

Prev Up Next