Skip to main content.
home | support | download

Back to List Archive

Re: Getting older .cgi scripts to work with

From: Andrew Lord <andrewlord(at)>
Date: Wed Jun 05 2002 - 19:45:44 GMT
Hello again Bill,

Thankyou very much for the work you did on that script and for the guidance 
on approaches to debugging. 

I have since spent many more hours doing 'perl tutes' and I think I understand much of what you've done in 
makewordlist().  But so that I can better understand the script, I wondered 
if you'd mind explaining some facets for me:

1. what is "@_"     ?
2. what does "|" do in "open ($fh, "$swish -k $letter|" ) or"   ?
3. Is "gensym;" some sort of perl-predefined operator  ?  
4. what role does "[1]" serve in "if ( my $words = ( split /\s*:\s*/ )[1] ){" 

I've not been able to complete debugging the various sub routines (employing 
"use strict"). This seems to be because it's not always as simple as putting 
a "my" in front of the word in question.  Please excuse my lack of knowledge 
in this area, but are there other equivalent words to "my" that should be 
used in contexts such as those below (subroutines from dictionary.cgi saved 
as bm1.cgi & bm2.cgi)? 

Running "perl -c bm1.cgi" gives the following Errormessages:  

"Global symbol %input requires explicit package name at 
bm1.cgi line 5"
"bm1.cgi had compilation errors."

//////////////  bm1.cgi ////////////////////////////////////////////////////
#!/usr/bin/perl -w     
use strict;	

my $searchscript = 'simple2sdev.cgi';
my $swishtags = "index=$input{index}&results=0&search_tags=H&search=";

sub tableize {
    my($columns,@elements) = @_;
    $rows = int(0.99 + @elements/$columns);
    # rearrange into a pretty table
    $result = "<TABLE CELLPADDING=2>";
    for ($row=0;$row<$rows;$row++) {
        $result .= "<TR>";
        for ($column=0;$column<$columns;$column++) {
            my $result .= qq|<TD><A HREF="my $searchscript?my $swishtags| . 
			$elements[$column*$rows + $row] . '">' .
			$elements[$column*$rows + $row] . '</A></TD>';
        $result .= "</TR>";
    $result .= "</TABLE>";
    return $result;
# end of file

Running "perl -c bm2.cgi" gives the following Errormessages: 

"Global symbol %input requires explicit package name at bm2.cgi 
line 20"
"Global symbol %input requires explicit package name at bm2.cgi 
line 22"
"bm2.cgi had compilation errors."

//////////////  bm2.cgi ////////////////////////////////////////////////////

#!/usr/bin/perl -w
use strict;

sub getinput{
	my ($qstring,$i,$value);
	my @querylist;
		read(STDIN, $qstring, $ENV{'CONTENT_LENGTH'});
		$qstring = $ENV{'QUERY_STRING'};
	# decode input into hash table
	@querylist = split(m'&', $qstring);
	for $i (@querylist)
17		(my $key,$value) = split(/=/, $i);
18		my $value =~ tr/+/ /;
19		$value =~ s/%(..)/pack('c',hex($1))/eg;
20		$input{$key} = $value;
21	}
22	return %input;


I have been able to run sub makewordlist() as a separate file, as you 
suggested.  A good and useful approach.  The script works without a hitch 
from the command line where it does an excellent printout of words for the 
default or query letter from an index.swish-e located in the same directory 
as the script.  However, when makewordlist() is transplanted into 
dictionary.cgi, and dictionary.cgi is then run in the browser, it doesn't 
seem to matter which index is selected from the drop-down menu or which 
letter button is clicked; the script only ever reports the list of words 
corresponding to the default letter, but only with respect to the content of 
an index located in the same directory in which the script resides.  
Nevertheless, the words are correctly hyperlinked.  

The lines of the main program that I have changed are shown below at the 
lines marked as "###" (Main Program for dictionary.cgi).  Perhaps if you 
could suggest how to correct the problems I've described, I'd welcome your 


Andrew Lord

////////  Main Program for dictionary.cgi  //////////////////////////////

$searchscript = 'simple.cgi';
# Absolute path and command to execute the SWISH searcher   
$swish = '/home/httpd/html/swishdev/src/swish-e';

#- Standard Stuff you won't need to edit -----------------------------#
# configure the sections from the configuration files.
my $swishdir = "$ENV{'DOCUMENT_ROOT'}/SWISH/swish";
# find the configfile that belongs to the index
@sections = glob("$swishdir/*.conf");
foreach $section (@sections){
	open CONF,$section;
		if (/^IndexFile\s(.*)$/){
			$indexfile = $1;
			$parts{$indexfile} = $1;
		if (/^IndexName\s(.*)$/){
			$indexname = $1;
			$indexname =~ s/"//g;
	 		$parts{$indexfile} = $indexname;
		# ReplaceRules should no longer be quoted for this to work
		s|["']||g; # if they still are they're trimmed
		if( m|ReplaceRules\sreplace\s(\S*)\s(\S*)|i){
					$replace{$2} = $1;
	close CONF;

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

# Parse web input   
# When the pattern is used act accordingly
$swishtags = "index=$input{index}&search=";
$pattern = $input{'l'};
$index = $input{'index'};
# data filled in so do da thang!
# generate the select with the sections

$select = &makeselect;				###
if($pattern){					###
use Symbol;					###
my @stuff = makewordlist( shift || 'b',		### 
'/home/httpd/html/swishdev/src/swish-e' );	###
        &makewordlist;				###
        $wordlist = (&tableize(6,@stuff));		###
Received on Wed Jun 5 19:49:22 2002