Exploiting the Information World
ITEC-2110 - Fall 2000

These exercises are part of your grade!

For each exercise that you complete during class you must make record your results to show the TA (or show the TA your screen). If you don't have time to complete everything during class you must record your results and give them to the TA by the next class meeting.

EIW Exercises: Perl CGI


1. Installing and running a simple perl CGI

If you got a Perl CGI program running last time (as part of Exercise 12), skip this and go to: 2. Getting at the query string.

These instructions assume you are using the Xitami web server

The file simple.pl is a simple perl CGI program that can help you learn how to install and run a perl based CGI with the Xitami web server. The CGI program simply sends back some HTML, it doesn't ever look for input from the browser (a query string). Here is a list of the steps necessary to install this program.

  1. Get the perl script simple.pl and save as a file on your computer.
  2. Test the script with your perl interpreter in a DOS window before attempting to install as a CGI program.
  3. The Xitami web server will only run CGI programs that include the term "cgi-bin" in the path. You can copy the program to the main cgi-bin directory or you can create your own directory (or complete folder/directory heirarchy) as long as the name of one folder/directory is "cgi-bin".

    On my computer, I've set the main HTML directory to be c:\dlh\it\web instead of the default directory c:\Xitami\Webpages. I've also created the directory c:\dlh\it\web\eiw\cgi-bin as a place to put CGI programs. Once I've put simple.pl in to the folder c:\dlh\it\web\eiw\cgi-bin. I can run the program by pointing the browser to the URL: http://127.0.0.1/eiw/cgi-bin/simple.pl.

  4. Make sure the CGI program has the correct first line. The Xitami web server looks at the first line of the file to find out that the file is a perl script, and where to locate the perl interpreter. Assuming you used the default perl installation, the first line of the perl script must be:
    #!/perl/bin/perl
    
    If the first line is missing or incorrect the web server will not run your perl CGI, it will attempt to send back the content of the file!
  5. Run the CGI program from a browser. In my case, I just enter the URL: http://127.0.0.1/eiw/cgi-bin/simple.pl. The URL you need depends on the path from the main HTML directory to your program. You must install the program somewhere below (within) the main HTML directory or the web server will not be able to find it. The web server looks for all resources (files) within the main HTML directory.

    To change (or simply find out what the current setting is) the location of the main HTML directory you should go to the web server administration page using the URL: http://127.0.0.1/admin. Note that you need to know the username and password you entered when installing the web server. If you have problems - check this list:

    Still can't get it working? Check the Xitami documentation on running CGI programs or ask for help.

    2. Getting at the query string

    Inside your perl program you can get at the query string with the following code (this works for GET requests, not POST):

    
    $q = $ENV{"QUERY_STRING"};
    
    

    In that above code, if a query was sent as part of the GET request that started the CGI program, the query string will end up in the variable $q. If there was no query string the variable $q will be empty.

    1. Write a CGI program that sends back to the browser an HTML document that includes whatever was sent in the query string. For example, your document could send back: "<H3>Your query string was ", followed by the actual query, followed by "</H3>".

    2. Write a CGI program that treats the query string as an integer value, and sends back a document that includes a hyperlink back to the same CGI. THe hyperlink should include a query string that has a value that is one larger than the value received. If the program name is "count.cgi" and it is called as "count.cgi?18" it should create a hyperlink that references "count.cgi?19".

    3. Create a CGI program that expects the query string to be the name of a file. The CGI program sends back the contents of the named file. You will need to know how to open and read from a file in perl - here is an example:

      # assume the variable $file holds the name of a file
      
      # open the file for reading, creates a "handle" named F we can use
      # when reading from the file.
      
      if (! open(F,$file) ) {
         # ERROR - open returned FALSE - the open did not work
          print("ERROR - FILE $file not found\n");
          exit;     #terminates the program!
      }
      
      # now read one line at a time from F, printing each line out
      while (<F>) {
      	print;
      }
      
      # close the file
      close(F);
      

    4. Create an HTML form and set the action to point to the CGI program that just sends back the query string. Make sure the form METHOD is GET. Try typing in different values for the form fields and submitting to your CGI.