Ïðèãëàøàåì ïîñåòèòü
Ãîðüêèé (gorkiy-lit.ru)

Exercise: Creating a Simple Customer Database with Perl

Previous Table of Contents Next

Exercise: Creating a Simple Customer Database with Perl

When you call a customer service center and finally make it through the Touch-Tone menus, the first thing the human being on the other end of the phone asks you for is your telephone number. Well, almost every time. Sometimes the customer service representative wants your customer number or even your Social Security number. What this person is after is something that uniquely identifies you to the computer he or she is using. These numbers serve as keys for retrieving information about you in a database. Sounds like Perl's hashes, doesn't it?

For this exercise, you're going to search a customer database. This program assumes that the database already exists, and it doesn't provide any way to update that database—yet. Here, you're going to allow the user to search on one of two different fields.

To begin this exercise, you need some data. Fire up your text editor, key in the text in Listing 7.4 (or something similar), and save it as customers.txt. Don't worry about the number of spaces between the columns or aligning them, as long as you leave at least one space between each column.

Listing 7.4. Sample Data for the Customer Program

Smith,John   (248)-555-9430 jsmith@aol.com

Hunter,Apryl  (810)-555-3029 april@showers.org

Stewart,Pat  (405)-555-8710 pats@starfleet.co.uk

Ching,Iris   (305)-555-0919 iching@zen.org

Doe,John     (212)-555-0912 jdoe@morgue.com

Jones,Tom    (312)-555-3321 tj2342@aol.com

Smith,John   (607)-555-0023 smith@pocahontas.com

Crosby,Dave  (405)-555-1516 cros@csny.org

Johns,Pam    (313)-555-6790 pj@sleepy.com

Jeter,Linda  (810)-555-8761 netless@earthlink.net

Garland,Judy (305)-555-1231 ozgal@rainbow.com


In the same directory, key in the short program in Listing 7.5 and save it as Customer. As usual, don't type the line numbers, and, if you can, be sure to make the program executable according to the instructions you learned in Hour 1, "Introduction to the Perl Language."

When you're done, try running the program by typing the following at a command line:


Customer


or, if you cannot make the program executable on your system,


perl -w Customer


Listing 7.5. Complete Listing of the Customer Program

1:   #!/usr/bin/perl -w

2:

3:   open(PH, "customers.txt") or die "Cannot open customers.txt: $!\n";

4:   while(<PH>) {

5:       chomp;

6:       ($number, $email) = ( split(/\s+/, $_) )[1,2];

7:       $Phone{$number} = $_;

8:       $Email{$email} = $_;

9:   }

10:   close(PH);

11:

12:   print "Type 'q' to exit\n";

13:   while (1) {

14:       print "\nNumber? ";

15:       $number = <STDIN>;  chomp($number);

16:       $address = "";

17:       if (! $number ) {

18:           print "E-Mail? ";

19:           $address = <STDIN>;  chomp($address);

20:       }

21:

22:       next if (! $number and ! $address);

23:       last if ($number eq 'q' or $address eq 'q');

24:

25:       if ( $number and exists $Phone{$number} ) {

26:           print "Customer: $Phone{$number}\n";

27:           next;

28:       }

29:

30:       if ($address and exists $Email{$address} ) {

31:               print "Customer: $Email{$address}\n";

32:               next;

33:       }

34:       print "Customer record not found.\n";

35:       next;

36:   }

36:   print "\nAll done.\n";


Listing 7.6 shows the output of the Customer program.

Listing 7.6. Sample Output from Customer

Type 'q' to exit

Number? <return>

E-Mail? cros@csny.org

Customer: Crosby, Dave    (405)-555-1516  cros@csny.org



Number? (305)-555-0919

Customer: Ching,Iris   (305)-555-0919  iching@zen.org



Number? q



All done.


Line 1: This line contains the path to the interpreter (you can change it so that it's appropriate to your system) and the -w switch. Always have warnings enabled!

Line 3: The customers.txt file is opened for reading on the filehandle PH. Of course, errors are checked for and reported.

Lines 4–5: The PH filehandle is read, each line being assigned to $_. $_ is chomped to remove the trailing newline character.

Line 6: The line (in $_) is split on whitespace (\s+). Surrounding the split statement is a set of parentheses, and brackets follow them. Because you're interested in only the phone number and email address from each line, you take a slice of the return values from the split. The two values are assigned to $number and $email.

Lines 7–8: %Email is used to store the customer record, the key being the email address. %Phone is used to store the customer record also, but keyed by phone number.

Line 10: This line closes the filehandle.

Line 13: This while loop encloses the portion of code that needs to be repeated. The statement while(1) is a Perl idiom that means "loop forever." To exit this loop, a last statement will eventually be used.

Lines 14–15: The phone number is read, and the newline character is removed.

Lines 17–20: If no phone number is available, these lines prompt for an email address.

Lines 22–23: If nothing was entered, this line repeats the loop. If either response was a q, then the loop is exited.

Lines 25–28: If a number was entered, and it is valid, line 26 prints the customer record. Control is passed back to the top of the block with a next statement.

Lines 30–33: If an address was entered, and it's valid, the customer record is printed. Control is passed back to the top of the block with next.

Lines 34–35: Either an address or a phone number was entered, and it was found not to be valid. These lines print an appropriate message and repeat the block with next.

This example demonstrates a few Perl features. Hashes are used for quick lookups of data based on a key. Because Perl implements hashes very efficiently, response time for a query should not become inefficient even if this program has thousands or tens of thousands of records in the hashes. Also, this program serves as a demonstration of program flow control using a simpleblock instead of other control structures (while, do, until, and so on).

    Previous Table of Contents Next