Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Perl programming

Perl is a programming language which is quite popular for cgi programming because of its powerful text processing facilities.

The syntax is a mixture of C++ and UNIX shell script language. It is fairly difficult, and it is not recommended to begin with Perl unless you have a good programming experience in other programming languages.

A Perl program is compiled every time it is run. Only the source code is stored, not the compiled code.

Perl programs will run on most servers, and are usually called through the CGI interface, for example from a HTML form.

Note that a Perl script must have UNIX-style linefeeds. If you have edited a Perl script on a PC, then you must transfer it to the server by FTP in ASCII mode in order to convert the linefeeds. The filename should have extension .cgi.

Example

This example is a simple pocket calculator
 
First operand: 
Second operand: 
+
-
*
/

The program has three input parameters: the first operand named a, the second operand named b, and the operator, named opera, which is 1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division.

Perl code:

#!/usr/bin/perl
##############################################################################
# Perlcalc.cgi                                                AgF 2000-07-14 #
#                                                                            #
#  This is a simple pocket calculator to be called under CGI.                #
#  The program can add, subtract, multiply, or divide two operands           #
#                                                                            #
#  Input:                                                                    #
#  a:     first operand                                                      #
#  b:     second operand                                                     #
#  opera: operator: 1=add, 2=subtract, 3=multiply, 4=divide                  #
##############################################################################

# Print cgi header
print "Content-type: text/html\n\n";


# Get Form parameters
&parse_form;
$a = $FORM{'a'};
$b = $FORM{'b'};
$opera = $FORM{'opera'};

# do calculation
if ($opera == 1) {
   # addition
   $result = $a + $b;}
elsif ($opera == 2) {
   # subtraction
   $result = $a - $b;}
elsif ($opera == 3) {
   # multiplication
   $result = $a * $b;}
elsif ($opera == 4) {
   # division
   if ($b != 0) {
      $result = $a / $b;}
   else {
      # avoid division by zero
      $opera = 0;}}

# define operator names
@OperatorNames = ('+','-','*','/');

# output HTML code
print "<html><head><title>Calculator Result</title></head>\n";
print "<h1>Calculator Result</h1>";
if ($opera > 0 && $opera < 5) {
   print "$a $OperatorNames[$opera-1] $b = $result";}
else {
   print "Not defined";}
print "</font></body></html>";


############################################################################
# Parse Form Subroutine                                                    #
# This subroutine gets POST-parameters and stores them into array FORM     #
############################################################################
sub parse_form {
   # Get the input
   if ($ENV{'CONTENT_LENGTH'} > 1) {
      # called by POST, read from STDIN
      read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});}
   else {
      # called by GET. read from QUERY_STRING
      $buffer = $ENV{'QUERY_STRING'};}

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);  $buffer = "";

   foreach $pair (@pairs) {
      ($nam, $value) = split(/=/, $pair);

      # Decode plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9]{2})/pack("C", hex($1))/eg;

      # Store all parameters in array FORM
      $FORM{$nam} = $value;}}

HTML code for form:
 
<form action="perlcalc.cgi" method="POST">
   First operand: <input type="text" size="20" name="a"><br>
   Second operand: <input type="text" size="20" name="b"><br>
   <input type="radio" checked name="opera" value="1"> +<br>
   <input type="radio" name="opera" value="2"> -<br>
   <input type="radio" name="opera" value="3"> *<br>
   <input type="radio" name="opera" value="4"> /<br>
   <input type="submit" value="Do it!"> 
   <input type="reset" value="Clear fields">
</form> 

Finding errors

Debugging a Perl program can be quite difficult. These are methods to find errors if your Perl script doesn't work:
Test your form
First, you must test if your HTML form works correctly. Insert action="/cgi-bin/test.cgi" in the <FORM > tag instead of your own program and see if the form gives the correct parameters. Note that parameter names are case sensitive.
 
Are the linefeeds UNIX style?
Be sure to transfer the Perl script from your PC to the UNIX server in ASCII mode.
 
Is the Perl script executable
Make sure the Perl script file has execute permission for everyone.
 
Does the filename have the right extension?
Your Perl scripts should have extension .cgi if they are called through cgi. Some servers also allow the extension .pl.
 
Is your program in the right directory?
Some servers require that programs be stored in the cgi-bin directory. This does not apply to the student server.
 
Syntax errors
Check for syntax errors by writing  perl -d YOURFILE.cgi  on the UNIX command line.
 
CGI header
The program should output a cgi header followed by a blank line before it outputs anything else.