#!/usr/bin/perl # The first statement in this script is, as usual, the one that # requires cgi-lib.pl. But we are NOT calling the 'ReadParse' # subroutine immediately, because it will be used later, in an # 'if' statement. require "cgi-lib.pl"; # The $data_file variable is the name of the file that stores # the URL's, page titles, and key words for your site. It can # be a relative or absolute path, but it's easiest to put the # data file in the same directory as this script, so that you # need only list the file's name. $data_file = "database.txt"; # No matter what happens, the script will be returning an HTML # file as output, so print an HTTP header: print "Content-type: text/html\n\n"; # Call the 'ReadParse' subroutine (from cgi-lib.pl). If nothing # is returned ("!&ReadParse" means "NOT &ReadParse"), call the # 'NoQuery' subroutine (at the end of this script). This is, of # course, what happens the first time the script is called, with # no query. if ( !&ReadParse ) { &NoQuery; } # Everything from this point on is executed if 'ReadParse' # *does* return something; i.e., a query WAS passed to the # script. Note that 'ReadParse' was called from the 'if' # statement, so our form data (if there was any) has been # dumped into the %in array. else { # Open the database file into a filehandle called DATABASE; # if that doesn't work, call the 'FileError' subroutine. The # || operator (logical OR) is being used for flow control. open ( DATABASE , $data_file ) || &FileError; # Read the contents of DATABASE into the @data_lines array. @data_lines = ; # Go through the contents of @data_lines one line at a time. foreach ( @data_lines ) { # Split the current line ($_) at the semicolons, and put # all of the pieces into an array named @phrases. Note that # this array will change each time a new line is read from # the @data_lines array. @phrases = split ( /;/ , $_ ); # This 'if' statement looks for the search key in the # @phrases array. @phrases need not be mentioned by name # here, because it is the current input ($_), which is the # default pattern search space). if ( /$in{'query'}/ ) # If the key was found, then add a line to the variable # called $found. The period is a text operator that # simply concatenates two text strings. $phrases[0] is # the first piece of a database line (which is a URL), and # $phrases[1] is the second piece (a page's title). If # the search key is found in any more lines in the # @data_lines array (on future passes through the # 'foreach' loop), they will be added to the end of # $found, so it will end up as a list of one or more HTML # links preceded by a
  • tag. { $found = $found . "
  • $phrases[1]
    \n"; } # This curly bracket ends the 'foreach(@data_lines)' loop. } # Print out the results of the search. Whether something was # found or not, the top of the output page will be the same. # The search query is included in the title of the page. # (By the way, these lines are not indented because everything # between the ENDOFTEXT markers is taken literally, and we # don't want those extra spaces.) print < Search Results for "$in{'query'}"

    Search Results

    Your search for $in{'query'} returned:

    ENDOFTEXT # If the search key matched any items in the database, then # the $found variable will exist, so print out its contents, # enclosed in a

      tag (because $found is a series of
    • # list items). if ( $found ) { print "
        \n$found
      \n
      \n"; } # If nothing was found, say exactly that: else { print "Nothing...sorry!\n

      \n


      \n"; } # And this bracket finally closes off the else statement # that was started when 'ReadParse' was called and was not # empty: } # Finish off the HTML page. Every page will end with the # search input box, whether anything was found or not. print < Search for: (Searching is case sensitive) ENDOFTEXT ################################################################ # The 'FileError' subroutine is called when the script has a # problem opening the data file (either the file was not # found or it was not readable). The $! predefined variable # is the current error message. After printing out a page with # the error message, the script immediately exits. sub FileError { print < Error! Can't open database file ($data_file): $! ENDOFTEXT exit; } ################################################################ # The 'NoQuery' subroutine is called when no arguments are # passed through 'ReadParse', either because a blank form was # submitted or because the script is being called for the first # time, and there has been no opportunity to enter a query. # All this subroutine does is print out the top of a new # search form; the actual input box is in the main program, # because that will always be printed, regardless of what # happened with 'ReadParse'. sub NoQuery { print < Search Form

      Search this site

      You can search this site's URLs, page titles, and key words.

      ENDOFTEXT }