The Internet Encyclopedia (Volume 3)

(coco) #1

P1: C-149-Stotts


Perl WL040/Bidgolio-Vol III Ch-04 August 14, 2003 11:22 Char Count= 0


NETWORKPROGRAMMING INPERL 47

use LWP::UserAgent; # imports other
modules too
$client = new LWP::UserAgent();
$acmeReps = new URI::URL('www.acme.com/
reports/index.html');
$outHead =
new HTTP::Headers(User-Agent=>'MyBot
v2.0', Accept=>'text/html');
$outMsg = new HTTP::Request(GET, $acmeReps,
$outHead);
$inMSg = $client->request($outMsg);
$inMsg->is_success? {print $inMsg->
content;} : {print $inMsg->message;}

The network connections and message formatting to
HTTP protocol requirements is handled transparently by
the LWP functions. Here, the client causes a request like
this to be sent to the Web server at http://www.acme.com:

GET/reports/index.html HTTP/1.0
User-Agent: MyBot v2.0
Accept: text/html

If the requested file is not there, the response from the
server will be an error message. If it is there, the response
will contain the HTML file for the “bot” to process in some
fashion.

Database Use
Many Web sites depend on databases to maintain per-
sistent information—customer data, statistics on site us-
age, collection of experimental results, medical records.
Perl has several ways a CGI script (or any Perl script,
Web-based or otherwise) can store and retrieve database
records.

Module “DBM”
Perl comes with a module called DBM (database module)
that contains functions implementing a built-in database
structure. DBM treats an external data store internally
as hashes, or key/value pairs. This internal database is
intended for simple, fast usage; searching requires as few
as three lines of script. The method is convenient for fast
retrieval, even from very large databases, when there is a
unique key for searching (e.g., ISBN numbers). It is not
as useful for complex data or queries:

dbmopen (%db, $database, 0400) || die "Can't
open DB"; #open read only
for ($k=$max; $k< $max+20; $k++){print
"$k $db{$k}"} #get and print data
dbmclose (%db); #close database

In this example, we know the index values are numbers
and are unique. The database looks to the Perl script like
a hash, so the associative array variable%dbis used to get
the data values from it.
One useful feature Perl provides is DBM filters. These
are small data transformation routines, written by a pro-
grammer to manage situations where the format of the
data fields in a database is not quite compatible with the

form needed by a script. Rather than put small data ma-
nipulation code chunks scattered throughout the script,
or write and call extra functions, DBM filters can be at-
tached directly to the fields of a database; data transfor-
mation then takes place automatically as field values are
moved into and out of the database. This feature makes
the code using the database easier to read and less error
prone due to less code replication.

Module “DBI”
For more advanced applications, a relational database is
often more desirable than the simple structure of DBM.
For this Perl provides the DBI module, or data base in-
terface. DBI is designed to hide the details of specific
database systems, allowing the programmer to create
scripts that are general. The interface allows expressing
SQL queries, executing them against a specific database,
and retrieving the results. The DBI module does not con-
tain code specific to any database vendor’s product, but
it does have references to numerous vendor-specific mod-
ules called DBD’s (database drivers). A DBD module will
contain the detailed code needed to communicate with a
specific brand of database system.
Figure 1 illustrates the relationship among the execut-
ing script, the DBI, the DBD, and the physical database.
When a Perl script invokes a DBI function to execute a
query, the query is routed to the appropriate DBD mod-
ule according to how the DBI database handle was opened
(as an Oracle DB, as an Access DB, etc.). The DBD module
communicates with the actual files or tables of the phys-
ical database system and produces query results. These
results are communicated back to the DBI module, which
relays them back to the user’s Perl script. This layer of
indirection gives Perl scripts a generality that makes mi-
gration from one physical DB system to another relative
painless.

Module “ODBC”
In addition to the DBI module, programmers wishing to
write Perl scripts that interface to external databases, such
as Access or Oracle, can obtain an ODBC compatibility
package as a free download from several third party dis-
tributors. This module contains functions that implement
the ODBC standard database interface. Perl scripts writ-
ten to use this standard can then work with any relational
database under them, as long as that database has its
own ODBC interface. Almost all major relational database
vendors provide an ODBC implementation for their prod-
ucts. ODBC provides the same advantages as DBI, but the
ODBC standard was designed outside Perl and is available
in many other programming languages as well.

Processes and IPC
Although the Web is the major Internet application, it is
certainly not the only one, and Perl includes facilities to
support general network applications. Although not de-
signed specifically for concurrent or multiprocess compu-
tations, Perl does support various forms of processes, in-
terprocess communication (IPC), and concurrency. (Perl
having been born of Unix, this section is heavy on Unix
process concepts such as pipes and forking.) Processes are
Free download pdf