Lecture Notes Four: Perl, cgi-bin, CGI, %ENV, QUERY_STRING, GET, more Perl.

We start with Perl.

We discuss the incremental development of the following program that

This is really very easy:

#!/usr/bin/perl
 
while ($x = <STDIN>) {
  ($com, $arg) = split(/ /, $x); 
  print "You have typed: $x"; 
  if ($com =~ /^bye/i) { print "Good-bye!\n"; exit; }
  elsif($com =~ /^add/i) { $acc += $arg; print "Acc is now $acc\n"; }
  elsif($com =~ /^sub/i) { $acc -= $arg; print "Acc is now $acc\n"; } 
  else { print "Acc stays $acc\n"; } 
}
We write this program and run it.

We thus introduce

We later discuss how we can implement something similar with CGI and how the implementation method is fundamentally different. We first return to the helloFive context of last time.

Last time we developed a simple hello script. Its output was not sophisticated.

The output was coded in HTML, and the only difference between it and a similar (identical, in fact) file was that the output of the script had to start with a label that was specifying what type it had:

Content-type: text/html
followed by a blank line (hence the \n\n following it).

We then extended this script to allow for variable output from it.

We also started talking about the misterious printenv you found in your cgi-bin directory.

And we said associative arrays (hashes) are important in Perl.

The purpose of today's lecture is to provide enough information to allow you to implement a script with the following functionality for your next assignment (which has two parts).

The lecture on Thursday will clarify the second part of that assignment.

Here's the prototype for the first part of assignment #2.

Notice that just like helloFive:

Unlike helloFive: It will be our purpose to understand the mechanism that makes this possible.

Here's what we will need to get this done:

We'll try to touch all points on this list by developing an example that builds on printenv.

Once that is done, with the Thursday lecture we will move to:

That will enable us to implement the second part of assignment 2, for which we also have a prototype.

Thus we return to the original calculator that started these notes.

After that we can define CGI, and implement a function readParse that captures that definition.

We now start developing the helper program for the first part of the next assignment.

Here's the script we will develop in class:

#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

print "<html><body><pre>"; 

$string = $ENV{'QUERY_STRING'}; 

foreach $key (keys %ENV) {

  if ($key eq $string) { 
    print $key, " --> ";
    print $ENV{$key}, "\n";  
  } else {
    print qq{<a href="/cgi-bin/circular?$key">$key</a>}, "\n"; 
  }

} 

print "</pre></body></html>"; 

Try the script above.

Did you notice the circular in the code above?

What does it mean? What does it do? Why is it there for? What, if anything, is it?

How can we take it out of the script's code without disturbing the functionality of the script?


Last updated: Sep 6, 2001 by Adrian German for A348/A548