5.5. POST Example: License Plates

Second only to the issues
surrounding tattooing and tattoo removal, the hardest decision one
ever has to make is, upon moving to California and buying a
convertible, what personalized license plate should one get? In the
past, this was a slow and embarrassing process, requiring one to go
to the Motor Vehicles office, shuffle up to the clerk, and meekly
request "HOTBABE," only to receive
the crushing news that someone else has, somehow, already thought of
that and taken it as her own personalized license plate. While there
are 66,220,518,000 possible combinations,[2] it is apparently a devoted pursuit of
the state's 30-odd million residents to think of
personalized license plates. As with Internet domain names, if you
can think of it, someone probably already has it.

[2] This is
based on the formula: $c+=35**$_-10**$_for2..7;print$c;. (The 35 is because letter O is treated as
digit zero. The 10 is because all-digit plates are not
allowed.)

But now the California Department of Motor Vehicles has understood
our plight, and has put up the web site plates.ca.gov so that we can sit at home and
use the Web to see which of our license plate ideas is available. It
has a simple HTML form interface, shown in Figure 5-2.

Figure 5-2. California License Plate Search

However, it's so draining to have to plant the mouse
in the search box, type "PL8DV8" or
whatever other license plate you want, mouse over to the submit
button and press it, see the next screen report either
"Plate configuration PL8DV8 is
unavailable" or "this plate is
tentatively acceptable and available," then mouse
over to the Back button, press it, and so on for every possibility
that occurs to us. Just as a true power user would never use the web
interface to whois but would instead insist on
the command-line tool, we too would be happiest with a command-line
interface to this license plate search form.

5.5.1. The Form

Viewing the source of the search form at http://plates.ca.gov/search/ shows that,
omitting some table-formatting codes, it really just consists of:

From what we learned earlier about how different kinds of form
elements produce different kinds of pairs, we can deduce that filling
"PL8DV8" in the
type=text box, then pressing the
"Check Plate Availability" button
will cause two form pairs to be submitted:
plate=PL8DV8 and search=Check Plate
Availability.

In each case, the first part of the form pair comes from the
element's name attribute. With
the first pair, we follow the rule for text input elements, and get
the value from whatever the user has typed into
that box (or whatever is there by default). With either submit
button, we follow the rule for type=submit
elements and make a form pair from the value
attribute (if there is such an attribute and if this is the button
that the user is pressing in order to submit the form).

5.5.2. Use formpairs.pl

We can save a local copy of the
form's HTML source and edit the form
element's action attribute to
point to some server where we've set up as a CGI the
formpairs.pl program from earlier in this
chapter. The form element will then read:

Knowing this, it's simple to write code that takes
an argument from the command line and puts it into
$plate, performs the above POST request, then
checks the response. Example 5-3 is the complete
program.