www.fachtnaroe.ie



Web This site

Using Perl Online

[Programming Home] [Input (2)] [Process (3)] [Output (1)]

Processing

One of the first things that the script must do is accept the input generated by the form. There are many ways to do this. However, one of the perl maxims is "don't re-invent the wheel". In this case that maxim means that this task of taking input from a form is so common that rather than write our own code from scratch, we can use a perl module that already exists for this commonly carried out task.

We shall use the aptly named CGI module. Using it is easy. There are 3 steps:

  1. Declare to perl that you require the module
  2. Call a function in the module to process the form input
  3. (For tidyness & easy of typing) Move the form input from the CGI processed form to other variables

The CGI module will return the values in the input boxes in the form of pairings of the input form box names, and the values entered. This is fine, but slightly unsightly, hence the 3rd stage mentioned above. The original HTML was:


<br />Enter a number: <input type="text" size="10" name="first_num">
<br />Enter a number: <input type="text" size="10" name="secnd_num">
<br />Enter a number: <input type="text" size="10" name="third_num">

From this we remember that our input boxes names were (as shown above) "first_num", "secnd_num" and "third_num". These will be the three values we'll move into other variables.

The script

The simple addition script, with very basic output, looks like this:


#!/usr/bin/perl
use CGI; # Tell perl about the CGI module
CGI::ReadParse(); # Tell the module to 'parse' the form
print "Content-type: text/html\n\n"; # This tells the Web Server what type of data it's dealing with
# Tidy up the variables received.
$num1=$in{first_num};
$num2=$in{secnd_num};
$num3=$in{third_num};
$final_answer = $num1 + $num2 + $num3;
print "<html><body>The answer is $final_answer.</body></html>";
# End of script

Running the script

With the script in place we enter some sample data and then click...


...and get our answer!


You should hopefully find the output is correct. A script error will generated a code 500 (internal error) message. Obviously if this happens, check your code. Also make sure the execute bit is set - this is an executable script after all. A short way of doing this is to right click on the file in the FTP window and choose CHMOD.

Sample Form & Script

Click here to see the sample form and script in operation.

Simple Modifications

Saving Form Input

Back to the main Programming page.


[Programming Home] [Input (2)] [Process (3)] [Output (1)]

Last updated: 20120108-18:14
back to top
Fachtna Roe, Senior College, Central Technical Institute, Clonmel, Ireland.