PERL


Contents


Introduction

Perl is a scripting language that can be used to write CGI scripts.

It is a language for easily manipulating text, files and processes. Structurally it looks quite a lot like C , it has the curly brackets and similar conditional operators. It also has powerful text manipulation features through the use of regular expressions which though confusing at first are pretty damned useful for script processing.


Using Perl

Using the perl library provided, your script has to do the following basic things

Initialisation


   #!/usr/local/bin/perl

   require "www_lib.pl";	#include the perl library

   $my_url=&get_this_URL();	#find out what the current URL is

   %MYPAIRS=&PARSEFORM();	#get the pairs

The PARSEFORM sub-routine ensures that no shell-escapes are received by the processing part of your script and splits the input into and associative array that contains name,value pairs.

which activity


   @fields=keys %MYPAIRS;

   if ( @fields == 0 )

   {

     # no fields were present which means this is the display part

     &show_front_page;

   }

   else

   {

     # fields were present ,decide on what to do

     if ($MYPAIRS{'button1'})

     {

        # processing when button1 is pressed

     }

     elsif ($MYPAIRS{'button2'})

     {

        # processing when button2 is pressed

     }

     #

     # etc and so on

     #

     else

     {

        # exhausted all other possibilities, bad input

     }

   }

   exit;

The input_key is the name of a field on your form that determines what processing occurs. If your form contains several buttons each with a different action you can use the name of the button to decide what the script should do.

Display


   sub show_front_page

   {

     &PRINT_HEADER("title","");

     print "

      <form method=POST action=\"$my_url\">

      [the inputs that make up your form go here]

      </form>";

     &PRINT_FOOTER;

   }

The $my_url variable stores the url the user sees for the script. Using it in the ACTION allows the script to re-use itself for processing.

process

Simply process and display the results in whatever form is most appropriate.

Examples


Searching

Sooner or later the number of documents on your server is going to become so large that you are going to need a mechanism to search those documents for meaningful data.

There are many varied ways of doing this, most involve the use of full text database engines to index all the unique words in your documents so that the documents that they are in can be readily retrieved.

  1. Index the documents
  2. use a cgi script (in perl?) to search the indices.

You could use

THere are doubltess many more search mechanisms, but I reckon that GLIMPSE is currently (November 1995) has the best result set. So I have provided two scripts to index and retrieve data from the indices. You will need to modify the scripts for your own site.