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


You need to change the guts of the search next. It’s actually quite simple to turn your previous test for string equality into two regular expressions linked with the logical AND operator:

   # Check the fields for a match; display the entire
   # entry if it matches, or get another if not.

           if (($InfoArray[$LastName] =~ /$SearchTerms[0]/)
                   && ($InfoArray[$FirstName] =~ /$SearchTerms[1]/))
               {
               print <<ALLDONE;

Because of the && linking the two regular expressions, the test can only succeed if both statements are true. If either of the form fields was left blank, the value of $SearchTerms[] in the regular expression will match anything, so that part of the test will always be true.


NOTE:  The use of real numbers as subscripts, as in $SearchTerms[0] and $SearchTerms[1], is OK but only marginally so. In the latest version of search.pl, we know that only two variables can come from the form and we know the order in which they will arrive. However, things won’t always be this simple. It is generally bad programming practice to hard-code numbers when you can use variables instead.

Finally, the new search.pl needs a change at the bottom, where a message is displayed if the search fails entirely.

   # If the $Found flag is still 0, put up a message.

       if (!$Found)
           {
           print "<H2 ALIGN=\"CENTER\">Sorry. No matches found for ";
           print "$SearchTerms[0] $SearchTerms[1]</H2>\n";
           }

   #                    End search.pl

As you can see, this conditional expression only prints both search terms in the message if the $Found flag is set to false.

Now, invoke search.html from your Web browser, fill out the form and submit it. From the example given in Figure 12.5, the result looks like the page in Figure 12.6.


Figure 12.6:  The results of the search using both names

Searching in More Complex Ways

The version of search.pl that you just completed is still pretty simple. But it provides a foundation you can build on to do more complex searches of databases. As long as the data in a file are structured in some way that is known to you, you can duplicate the structure in a Perl script, read the records from the file into your local structure, and do whatever you want with it.

If you use your imagination, you can extend the basic concepts you've learned in this skill to much larger projects—an employee databases, for example, or a product inventory. If you can think it up, chances are you can implement it in Perl. Your Web browser and CGI give you a handy way to submit and display the information nicely, even if it's just for your own use.

Programming for the Internet

Network programming is an art that is both arcane and frustrating, but it has its rewards, too. If your program can get out to the network, you’ve left the bounds of your own computer. If the network is connected to the Internet—itself nothing but a worldwide network—then your program knows no bounds.

Perl contains a rich set of TCP/IP networking functions, all of which were copied directly from the C-language sockets library. This library of networking tools was originally developed in the early 1980s as part of what now is known as BSD (or simply Berkeley) UNIX, with BSD standing for Berkeley Systems Development. The BSD project started as a semi-covert hack of the original AT&T Bell Labs UNIX kernel undertaken by a ragtag group of graduate students in computer science at the University of California. Many members of this group, which included the likes of Ken Arnold and Bill Joy, went on to become demigods in the UNIX world—legends of programming, as it were. So it’s not surprising that BSD UNIX became one of the two standard flavors of UNIX, along with AT&T’s System V. Most of the modern UNIX versions have incorporated the best aspects of System V and BSD.


NOTE:  Ken Arnold is responsible for such UNIX staples as curses, a terminal-independent library of screen formatting functions, and rogue, a primitive yet engaging Dungeons & Dragons-type computer game of which he is a co-author. He also uttered what may be the greatest programming aphorism in history. It goes something like this: “Every program can be shortened by at least one instruction. Every program contains at least one bug. Therefore, by induction, every program can be reduced to one instruction that does not work.”

Because networking hadn’t become quite the rage in the early ’80s that it is now, AT&T’s UNIX had no standard library of network functions. It was considered to be a task that should be tackled at the device-driver level; in other words, you had to roll your own networking tools. Our precocious group of Berkeley grad students disagreed with this philosophy.

At first, they attempted to shoehorn networking functions into the universal UNIX canon of input and output: Everything is a file. In UNIX, virtually anything that can be read or written to, including peripheral devices such as printers and serial ports, is addressed through standard file operations. You open it, you read from it or write to it, you close it. But the BSD group found that networking was too complex to fit into the neat package of file maintenance, so they developed the philosophy, and then the library, called sockets.

The idea is similar to the traditional concepts of file I/O with a subtle twist. A socket is something that you plug into, rather than open as you would a book or a file cabinet. Once the connection is made, the established operations of reading and writing come into play for input and output; it’s that initial connection that’s important and it’s the reason the Berkeley group had to develop a separate library of functions for networking.


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.