Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Save this code as search.pl and run the program from the command line, using last names that you know are in your guest book file and some bogus names, too. You’ll see something similar to the display in Figure 12.2.


Figure 12.2:  Searching the guest book from the command line

Dissecting the First Code Version

As search engines go, search.pl is a pretty dumb one. But it’s extendable, which is important. We’ve only built a foundation here.

You may have noticed that search.pl is very similar to the Perl script you wrote to display all of the entries in the guest book, guestbook.pl. The difference is that the new script contains no HTML code and only displays one entry—if the last name entered on the command line is found.

One new Perl concept pops up in the beginning of search.pl:

   # Put the command-line argument in a local variable.

       $search = $ARGV[0];

This code snippet shows how to gain access to command-line arguments in Perl. Every Perl script automatically gets a list called @ARGV, which is an array of any arguments that were typed following the script name on the command line; ARGV[0] is the first, ARGV[1] is the second, etc. In the case of search.pl, ARGV[0] should contain the last name the script will look up in the guest book.


NOTE:  The concept of an ARGV list—indeed, the very name—was lifted straight out of the C programming language, in which every program gets an array of pointers to strings called **argv. The main difference is that in C, argv[0] points to the program name and the first argument is in argv[1]. By the way, argv is short for argument vector, vector being a more important-sounding word for array.

The rest of search.pl should be familiar to you. The line of code that really drives the search is this one:

   if ($InfoArray[$LastName] eq $search)

This very simple conditional expression compares the last name from every record in the guest book with the string that is entered on the command line. It’s a straightforward comparison that does nothing fancy. In fact, using this comparison alone, even the cases of each character in both strings must match or the search will fail. Later on in this skill, you will make it more fancy, and this expression is where you’ll do it.

Taking the Search to the Web

As the first step in beefing up search.pl, let’s take it to the World Wide Web. You can start with an HTML form:

   <HTML>

   <HEAD>
   <TITLE>Search the Guest Book</TITLE>
   </HEAD>

   <BODY>
   <H1 ALIGN="CENTER">Search the Guest Book</H1>
   <HR>
   <FORM ACTION="/cgi-bin/search.pl" METHOD="GET">
   <CENTER>
   <H3>Search for last name: </H3>
   <INPUT TYPE="text" NAME="search_for" SIZE="24" MAXLENGTH="30">
   <P>
   <INPUT TYPE="submit" VALUE="Submit search">
   <INPUT TYPE="reset" VALUE="Clear form">
   </P>
   </FORM>
   </BODY>
   </HTML>

Save the HTML code as search.html in a directory accessible to your Web browser. When you call it up in your browser, it will look similar to the page displayed in Figure 12.3.


Figure 12.3:  Submitting the search through HTML

The most extensive changes required to search.pl are in the print statements that display the search results, which now have to be formatted in HTML. First things first, however. You need to tweak the top of the program a bit to get the search term through CGI instead of from the command line:

   # Get header files.

       require "c:/Program Files/Sambar/cgi-bin/GuestBook.pm"
           || die "Can't open guest book header file: $!\n";

   # Make a title for the page.

       $PageTitle = "Search results";

   # Get the search term through CGI and split it up.

       $SearchString = $ENV{'QUERY_STRING'};
       ($tag, $search) = split (/=/, $SearchString);

The variable $tag in this code snippet is just a throwaway; the one you want is $search, which contains the search term.

Now, immediately after opening the guest book file, set up an HTML header:

   # Open the guest book file; die if it's not available.

       open (GUEST_BOOK, $GuestBookPath)
           || die "Can't open guest book file: $!\n";

   # Start up an HTML page.

       HTML_Header ($PageTitle);
       print "<BODY>\n";
       print "<H1 ALIGN=\"CENTER\">$PageTitle</H1>\n";
       print "<HR>\n";

The rest of the program is unchanged, until we reach the point where the search results are displayed. This is a case, as in Skill 8 when you used print to output a large amount of PostScript code, where the Perl here (<<) operator comes in handy. In the following example, print will ship out everything up to the string ALLDONE:

   # Check the last-name field for a match; display the entire
       # entry if it matches, or get another if not.

           if ($InfoArray[$LastName] eq $search)
               {
               print <<ALLDONE;
   <TABLE WIDTH="50%">
   <TR>
   <TD VALIGN="top"><STRONG>First name:
   </STRONG></TD><TD VALIGN="top">$InfoArray[$FirstName]</TD></TR>
   <TR>
   <TD VALIGN="top"><STRONG>Last name:
   </STRONG></TD><TD VALIGN="top">$InfoArray[$LastName]</TD></TR>
   <TR>
   <TD VALIGN="top"><STRONG>City:
   </STRONG></TD><TD VALIGN="top">$InfoArray[$City]</TD></TR>
   <TR>
   <TD VALIGN="top"><STRONG>State:
   </STRONG></TD><TD VALIGN="top">$InfoArray[$State]</TD></TR>
   <TR>
   <TD VALIGN="top"><STRONG>Country:
   </STRONG></TD><TD VALIGN="top">$InfoArray[$Country]</TD></TR>
   <TR>
   <TD VALIGN="top"><STRONG>E-mail address:
   </STRONG></TD><TD VALIGN="top">$InfoArray[$EMail]</TD></TR>
   <TR>
   <TD VALIGN="top"><STRONG>Comments:
   </STRONG></TD><TD VALIGN="top">$InfoArray[$Comments]</TD></TR>
   </TABLE>
   ALLDONE

               print "<H3>Visited on ";


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.