Running SWISH-E via a CGI Script

As you can see, it’s really pretty simple to run the swish-e program from the command line. As it turns out, running it as a CGI script isn’t much harder. Example 13-1 shows a simple CGI script that does just that. This script, like all the other examples in this book, can be downloaded from the book’s web site, at http://www.elanus.net/book/.

Example 13-1. A simple CGI script to do web site searching via SWISH-E

#!/usr/bin/perl -Tw # search.cgi - simple web search script using swish-e use strict; use CGI qw(:standard); $ENV{PATH} = ''; my $swish_program = '/w2/s/www.socalsail.com/html/search/swish-e'; my $index_file = '/w2/s/www.socalsail.com/html/search/scs_index'; print header, <<"EOF"; <HTML> <HEAD> <TITLE>Search SoCalSail</TITLE> </HEAD> <BODY> <H1>Search SoCalSail</H1> EOF my $search_words = param('search_words'); if (defined $search_words) { # run the external swish-e program to get the search results $search_words =~ s/[^\s\w\-\(\)]+//g; # lose naughty chars if ($search_words =~ /^([\s\w\-\(\)]*)$/) { # launder for taint-mode purposes $search_words = $1; } my $command = "$swish_program -w '$search_words' -f $index_file"; my @results = `$command`; my $results = ''; foreach (@results) { # process each line of the search results next if /^(#|\s|\.|err: )/; # skip comments, as well as space, # dot, and error lines my($score, $path, $title, $size) = /^(\d+)\s(\S+)\s"(.+)"\s(\d+)$/; $results .= <<"EOF"; <LI><STRONG><A HREF="$path">$title</A></STRONG><BR> ...

Get Perl for Web Site Management now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.