#!/usr/bin/perl # # Random web search # Generates a random word or phrase (or adds a new one) from a list to be searched for on Google. # Example used on http://www.randomwebsearch.com # Dan Cederholm (dan@cederholm.org) # January 2003 # use strict; use CGI; # set parameters my $wordList = "./wordlist.txt"; my $site ="http://www.YOURSITEHERE.com"; my $q = new CGI(); my $mode = $q->param("mode"); my $newWord = $q->param("newWord"); my $rws = $q->param("rws"); # decide what to do based on $mode for ($mode) { if (/write/) { &modeWrite; } elsif (/add/) { &modeAdd; } elsif (/generate/) { &modeGenerate; } else { &modeDefault; } } # add new word sub modeWrite { open FILE,">>$wordList"; print FILE "$newWord\n"; close FILE; chmod 0777, "$wordList"; print "Content-type: text/html;\n\n"; print "

Random Web Search

New word \"$newWord\" has been added to the list. Back to RWS"; } # submit new word sub modeAdd { print "Content-type: text/html;\n\n"; print "

Random Web Search

"; } # generate random word sub modeGenerate { srand; open(FILE,"$wordList"); # put the file contents into an array my @file = ; close(FILE); foreach (@file) {chomp} # remove the newlines my $word = $file[rand($#file)]; # select a random line print "Location: $site/cgi-bin/randomWebSearch.pl?rws=$word\n\n"; } # default mode sub modeDefault { print "Content-type: text/html;\n\n"; print "

Random Web Search

Add a word
"; } exit;