CGI and Perl

Querying the License Plate Database

You need a few other functions for this example. The first is the one used to look up the license plate in the database; another is to print the information about that license plate; and the third is the one that sends e-mail notification to the owner.

The first function opens the database file and scans it for a license plate match (see Listing 7.12).

Listing 7.12. Subroutine to search for a specific license plate in the database.

sub findLicensePlate {
    my($licensePlate)=@_;
    my(%info);
    if (open(DATABASE, "< $DATABASEFILE")) {
       $srchStr="^(?i)$licensePlate\\<\\*\\>";
       while (<DATABASE>) {
          if (/$srchStr/) {
             ($info{`lic'},$info{`name'},$info{`email'},
              $info{`color'},$info{`make'},$info{`model'})=split(`<*>`);
             last;
          }
       }
       close(DATABASE);
    }
    return %info;
 }

The next function prints the information about a given license plate number, as shown in List-
ing 7.13.

Listing 7.13. Subroutine to print the information found about the license plate.

 sub printInfo {
    my($licensePlate)=@_;
    my(%info)=&findLicensePlate($licensePlate);
    if (defined($info{`name'})) {
       print "<P><B>Owner</B> is: $name<BR>\n";
       print "<P><B>E-mail Address</B> is: $email<BR>\n";
       print "<P><B>Color</B> is: $color<BR>\n";
       print "<P><B>Make</B> is: $make<BR>\n";
       print "<P><B>Model</B> is: $model<BR>\n";
    } else {
       print "<P>Sorry, that license plate number was not found";
       print " in our database<BR>\n";
    }
 }