puzzle.cgi

#!/usr/local/bin/perl -w

# puzzle.cgi
# A simple crossword puzzle helper using 'grep'
# WI course: Unix and Programming Skills for Biologists - March 2003

use CGI qw(:standard);
$query = new CGI;

if (param())
{
   # Get input from HTML form
   $in = param("in");
}
# Eliminate any leading or trailing spaces
$in =~ s/^\s*//;      # leading
$in =~ s/\s*$//;      # trailing

# Add anchors for word boundaries 
$in_word = "\^$in\$";

# Start printing the HTML output to STDOUT 
print $query->header('text/html');
print "<HEAD><TITLE>Crossword puzzle helper</TITLE><HEAD>
<BODY><CENTER><H3>Crossword puzzle helper</H3></CENTER>\n";

# <PRE> tag is needed to get one word per line in HTML file
print "<PRE><FONT SIZE=+1 COLOR=red><B>Matches for $in</FONT>\n\n";

# Run the grep command on the dictionary at /usr/share/dict/words
# The -i switch makes the match case-insensitive

open(GREP,"-|") || exec "/bin/grep", "-i" ,$in_word, "/usr/share/dict/words";
while (<GREP>)
{
   print "$_";
}
close GREP;

print "</PRE>\n";

# Finish output
print $query->end_html;