search-form-demo.pl: CGI program initiated by HTML form in search-form.html

#!/usr/local/bin/perl
#
# papers-search.pl: Search the AusWeb95 papers "database"
#
# Copyright (c) Martin Gleeson, March 1995

$QUERY = $ENV{QUERY_STRING};
$QUERY =~ s/\+/ /g;                      # Change +'s to spaces
$QUERY =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig; # Decode special characters

$database="/cern/unimelb/cgi-bin/papers.txt";    # database

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

if ( $ENV{REQUEST_METHOD} eq "GET") {

    @QUERY_LIST = split( /&/, $QUERY);
    foreach $item (@QUERY_LIST) {
            ($param, $value) = split( /=/, $item);
         $QUERY_ARRAY{$param} .= $value;
    }

    $search_pat = $QUERY_ARRAY{search};
    $search_field = $QUERY_ARRAY{search_on};

    if( $search_pat eq ""){
        print "<html><head><title>Error</title></head>\n";
        print "<body><h2>Error:</h2>\n";
        print "<p>You must enter a search term!!</p>\n";
    }
    else {
        print "<html><head><title>Search results</title></head>\n";
        print "<body><p>Search results for \"<strong>$search_pat</strong>\"
                in the database:<hr>\n<pre>";
        $matched = "FALSE";

        open(FILE,"$database");
        while( <FILE> ){
            chop;
            ($number,$name,$org,$email,$title)=split(/[\t]+/,$_);

            if($search_field eq "author"){
                $search_on=$name; }
            elsif($search_field eq "org"){
                $search_on=$org; }
            elsif($search_field eq "paper"){
                $search_on=$title; }

            if ($search_on =~ /$search_pat/i) {
                if( $matched eq "FALSE" ) {
                printf "%-25s %-80s\n<strong>%-30s</strong>\n%-10s <em>%-80s</em>\n<p></p>\n",
                              "Name(s)","Email Address(es)","Organisation","Paper","Paper Title"; }

                $email =~ s/ /,/g;   # <- Change spaces to commas in email addresses<

                         ### wrap up & space out mailto url around author's name  ###
                $combo="<a href=\"mailto:$email\">$name</a>";
                $size= 25 - length($name);   for( 1 .. $size){ $combo .= " "; }

                ###   print out the match  ###
                printf "<hr>\n";
                printf "%s %s\n<strong>%-20s</strong>\n%-10s <em>%-80s</em>\n\n",
                                                   $combo,$email,$org,$number,$title;

                $matched = "TRUE";
            }
        }
        print "</pre>\n";
        close(FILE);
        if ( $matched eq "FALSE") {
            print "<strong>Search produced no result.</strong><br>\n";
            }
        print "<hr><p><strong>Note:</strong> If you have a browser that has integrated ";
        print "e-mail facilities (e.g lynx or Netscape), you can select the name of the ";
        print "person to send them a message.</p><hr>";
    }
}
exit(0);

HTML 2.0 Checked!