george.cgi

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

######  A simple search engine to look for terms in a file  ######
######  WIBR - Unix and Programming Skills for Biologists  ######

# A local copy of the SGD file from
# ftp://ftp.yeastgenome.org/yeast/data_download/gene_registry/registry.genenames.tab
$bigFile = "/home/george/yeast/registry.genenames.tab.txt";

# Use the CGI module (required to execute a script from a web page)
use CGI qw(:standard);

# Use the CGI::Carp to help debug this CGI script
use CGI::Carp qw(fatalsToBrowser set_message);

# Create a new CGI object called $input
$input = new CGI;

# Set CGI::Carp module to print errors to browser
BEGIN
{
   sub handle_errors
   {
      $msg = shift;
      print "<h1>Uh oh</h1>Got an error: <PRE>$msg";
   }
   set_message(\&handle_errors);
}

# Get the data (the parameter "term") from the web page.
$term = param("term");

# Tell the browser what kind of file you're creating
print $input->header('text/html');

# Open data file to search for this term
open(DATA, "$bigFile") || die "cannot open $bigFile for reading: $!";

print "<PRE>"; # The HTML <PRE> tag helps print out plain text

###############  The action begins here  ###############

while (<DATA>)   # Read the file one line at a time
{
   if (/$term/)   # if $term is found somewhere on this line
   {
      print $_;   # print the line
   }
}
close (DATA);

###############  End of the action  ###############

print "</PRE>";

# Tell the browser you're finished sending data
print $input->end_html;