The Tools
The absolute earliest version of
perl you want to be working with is 5.004.
You may be able to get away with 5.003, but
perl 4 must be avoided. Perl 4 is considered
a security hazard and lacks many of the basic
functions we will need to write our guestbook
application. In addition to the standard perl
distribution, you will be needing the DBI module
and DBD::mysql module from CPAN. The DBI module
is a general API for accessing databases.
The DBD::mysql module can be considered a
plug-in for DBI, allowing DBI to talk to MySQL
servers. For the purpose of this demonstration,
we used a pre-compiled MySQL daemon from ftp.mysql.com.
To demonstrate the application, we will
assume that a copy of Apache or some other
webserver has already been properly installed
and configured.
The great thing about DBI is that it works with any type of database. If your organisation uses Sybase, for instance, you should be able to pick up a Sybase DBD driver and use it to run the perl code included with this article.
The System
Most any system can run a MySQL server; however, it is recommended that you use at least a low-end Pentium class machine. For this demonstration, I'll be using a Slackware Linux 4.0 machine on a Celeron 366 chip.
Slackware isn't necessary either; the techniques described below will work with any distribution. If you use a package manager then all the tools described should install automatically without much fuss, as they are mature products with high levels of stability.
Installing MySQL
Installing MySQL couldn't be easier.
After downloading the appropriate tarball
from my local MySQL mirror, in this case
mysql-3.22.23b-pc-linux-gnu-i686.tar.gz,
the
normal un-gziping yielded me the tar file.
Before un-taring your package, first decide
where you want to install MySQL. MySQL itself
comes ready-to-run out of the package. Hence,
you should untar MySQL in its final resting
place. After untaring, cd
to
the distribution directory and run
./configure
. This allows the
daemon to set up the appropriate tables and then
start up. You should see something like this
after running
./configure
:
Creating db table
Creating host table
Creating user table
Creating func table
Creating tables_priv table
Creating columns_priv table
...etc...
Starting mysqld daemon with databases from /usr/local/mysql/data
If you see
a message staying that
mysqld
has created all the tables and started
up the mysqld daemon without incident, then
congratulations; you now have a functioning MySQL
installation.
Installing the Perl Modules
The next step is to install the two necessary modules. The best way to go about doing this is to use the CPAN shell. CPAN, the Comprehensive Perl Archive Network, is the world's largest collection of perl modules. You first need to install DBI, the database interface module. This can be done by typing:
perl -MCPAN -e 'install DBI'
If perl should return an error such as
Unrecognized switch: -MCPAN
, it means you have
perl 4 installed. Fear not, though, your
administrator may have installed perl 5 later
on in your path. You may want to try
/usr/local/bin/perl -MCPAN
-e'install DBI'
/usr/bin/perl -MCPAN
-e'install DBI'
This command invokes the CPAN shell,
a tool that comes bundled with every version
of perl to help facilitate the installation
of modules. If this is the first time you`ve
used the CPAN shell, you will be prompted with a
few configuration questions. If you are running
CPAN shell as root, you are probably okay if
you select the defaults, and pick a CPAN mirror
close to you. If you are non-root, you will
want to specify
PREFIX=/your/home/directory
when asked for
parameters for
perl Makefile.PL
. It should look
something like this:
Parameters for the 'perl Makefile.PL' command? PREFIX=/my/home/dir
What this tells perl to do is install the modules inside your home directory, instead of the public perl modules directory. If all has gone well, you should see something similar to this:
/usr/bin/make
install -- OK
After you have a successful DBI installation, you will need to install the DBD::mysql DBI plugin. This can be done by executing:
perl
-MCPAN -e'install DBD::mysql'
You
will be asked a series of questions needed
for installation. When asked what kind of
drivers you wish to install, be sure to specify
'MySQL only.` When
asked if you want MysqlPerl emulation, it is
typically safe to say 'n
` When asked where your MySQL include
directory is, specify the directory that you
untared your MySQL distribution in. You can
safely hit <Enter> to the remaining
configuration options. Once again, if you
receive:
/usr/bin/make install -- OK
then you have installed DBD::mysql without incident. Congratulations.
Page 3: Setting up the Database