Perl language: Part IV

I/O through Unix environment variables, basics of Forms, CGI's

We have learnt how to get Input/Output through (a) Keyboard/Screen (STDIN, STDOUT) and (b) Files. In this lab, we shall see the third useful way for I/O: using Unix environment variables.
Also, we shall see how to create the simplest of Forms, and how a CGI program can be used for the basic communication between web clients and servers.


Example1.pl
#!/usr/local/bin/perl

%my_env = %ENV;

while ( ($key, $val) = each %my_env) {
  print $key, " = ", $val, "\n";
}

NOTES:
As you all know, your unix environment has a number of variables whose values have been set up by the system or by you. For example, you had set the value of the PATH variable through your .cshrc_user file. Perl can access each of your Unix environment variables, and stores the vairables and their current values in a hash structure called "ENV".
In this example, we read print the values of each of the environment variables.


Example2.pl
#!/usr/local/bin/perl

print ("My PATH vairable is set to the following value:\n");
print $ENV{'PATH'};
print "\n";


NOTES:
This is a simple example, showing that you can extract individual values for any of your environment variables.


Example3.pl
#!/usr/local/bin/perl

# here is a perl subroutine:

sub area {

  $length = @_[0];
  $width = @_[1];
  $area = $length * $width;
  return $area;
}


# here is the main program:

  print "Enter the length: ";
  $len = <STDIN>;

  print "Enter the width: ";
  $wid = <STDIN>;

  $a = do area( $len, $wid);

  printf( "The area is: %6.4f \n", $a);


NOTES:
This example has two interesting things:
1. It shows how to write your own subroutines (or functions) using Perl. Each subroutine must have a name (our's is called area ). A subroutine may take one or more arguments. area takes two arguments. When the subroutine is called from the main program (or from another subroutine), the values of the arguments are passed to the subroutine in a special array, whose name is "@_". The first element of @_ is the first argument, and so on..

2. In the main program, I use the printf function instead of the usual print function. printf can be used almost exactly the same as the C++ printf function. Notice how I can specify how I want the value of the variable "$a" to be printed. The directive '%6.4f' specifies that I want $a to be treated as a floating point number, and the printed output must be at least 6 digits, with exactly 4 digits after the decimal point.


Now, we are ready to write FORMS in HTML, and to write CGI's in perl, to respond to our FORMS !
Example4.
Part 1. The FORM

The usual method for a web client (example: Internet explorer, or Netscape Navigator) to communicate with the web server is to send a request for a particular document. This is done by typing the location (or, to be more precise, the URL) of the document you want in the "location" bar of the web client program. However, often we may want to send more information to the web server, based on which the server can take particular actions, and send us the results. The simplest method of sending some data to the web server is by the use of FORMs. A FORM can be a part of any standard HTML document.

Therefore, if you create a form, it must

(a) be inside an HTML document;
(b) This document must be placed in a location such that a web server can give it to a client on request (in the public_html directory under your home directory);
(c) This document must be read-open for the world.

Each FORM must have at least one item: a button that, when clicked, will send the data entered in any input fields of the form to the web server.

Since you want to send some data to the server, it is clear that the form must have at lest one more field, which allows the user to type some data into the form. Of course, you may enter as many different types of data into a form as you like, as we shall soon see.

Finally, when the form submits the data to the web server, you expect the server to somehow receive this data, and perform some operations based on this data. To do so, (a) your FORM must specify an executable program, located in the web server, capable to receiving, understanding, and processing the data presented through the FORM; (b) You must write the program. This program is called a CGI program, and is typically located inside the cgi-bin directory under your public_html directory.

The following is a simple example of a FORM. You may copy it and save it as an html file in your directory:

<HTML>
<FORM METHOD="get" ACTION="http://home-cgi.ust.hk/cgi-bin/cgiwrap/~YOUR_LOGIN/example.pl">
<p>Your Name:
<INPUT NAME="form_name" value="" size=10><br> <p>
<INPUT TYPE="Submit" value="Submit Query">
<INPUT TYPE="Reset" value="Reset"> <p>
</FORM>
</HTML>

NOTES:

Save this form into a file in your public_html directory. Make sure it is read open for all.

The form has three input fields:
The first INPUT field requests is a TEXT input box; its name is "form_name"; it will allow a user to type upto 10 letters of text.

The second INPUT field is of Type "Submit", which creates a button with the words "Submit Query" on it. A mouse click on the button will send the data entered into the "form_name" INPUT to the web server.

The third is a reset button, that will reset all the input fields values to their default values, for the user to re-enter the inputs.

Note:

(i) Replace the text YOUR_LOGIN by your login name (e.g. ie_abc). Do not remove the preceding "~" mark !
(ii) Replace the name "example.pl" with the name of the file with your CGI program.


Example 4.
Part 2. The CGI program

Now we are ready to write the CGI program that will receive the data from the FORM, and act upon it. The following is a simple example.

#!/usr/local/bin/perl

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

$data_from_form = $ENV{'QUERY_STRING'};

@data = split( /&/, $data_from_form);

# we are expecting just one element in @data, a string that looks like:
# form_name=ajay  .. if you typed "ajay" in the FORM.

@key_val = split( /=/, $data[0]);
print "<HTML> <BODY> \n";
print "<p>\n";
print "You had typed this name:";
print "<p>\n";

print $key_val[1];

print "<p>\n";
print "</BODY></HTML>";


Exercise:

1. FORMS and CGI

(a) Modify The FORM above to ask for two inputs: Length, and Width.

(b) Write a CGI program that will accept the data from the form in part (a), and output the following sentence: The Area of a rectangle with length= [INPUT LENGTH] and width=[INPUT WIDTH] is [AREA]

Your CGI program must print the user input values in place of [INPUT LENGTH], [INPUT WIDTH] and teh area in place of [AREA].