10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3 ■ DATABASES AND PERL 51The program incorporates the DBI in<strong>to</strong> the namespace with this line:use DBI;The available drivers are placed in<strong>to</strong> an array called @drivers with this line:@drivers = DBI->available_drivers;Finally, the array is expanded within the foreach loop and printed <strong>to</strong> STDOUT, producingthe output.As you saw from the output, the MySQL DBD is installed on this server. If you wanted <strong>to</strong>connect <strong>to</strong> a different type of database, you would need <strong>to</strong> obtain the DBD module from yourfavorite CPAN mirror or install it from your distribution’s reposi<strong>to</strong>ry. For example, Debian 3.0includes a number of DBDs, a listing of which is available by searching the reposi<strong>to</strong>ry with thecommand apt-cache search dbd.Some of the more popular DBDs include the following:• MySQL: As previously stated, MySQL is one quarter of the prized LAMP (Linux-Apache-MySQL-<strong>Perl</strong>) development platform that’s so popular around the world.• PostgreSQL: Another popular open-source database is PostgreSQL. The DBD forPostgreSQL is similar <strong>to</strong> that of MySQL.• ODBC: The ODBC DBD is commonly used <strong>to</strong> connect <strong>to</strong> databases that run on Windowssystems, such as Microsoft SQL Server and Microsoft Access, but the ODBC driver couldbe used <strong>to</strong> connect <strong>to</strong> virtually any database that offers ODBC connectivity.• Sybase: Another popular DBD is used with the Sybase database server. This server andthe DBD for it won’t be covered in this book. For more information about runningSybase on Linux and the DBD for Sybase, see http://www.peppler.org/.Data Source Names, Credentials, and AttributesA data source name, or DSN for short, is the information needed by the DBI in order <strong>to</strong> connect<strong>to</strong> the database. This information includes the DSN itself, as well as other informationsuch as the hostname of the server hosting the database, the name of the database, a portnumber, and so on. The exact information that you must supply depends on the DBD thatyou’re using <strong>to</strong> connect. For example, an Oracle DBD might require or accept differentparameters than the MySQL DBD uses.The DSN is a simple string, frequently s<strong>to</strong>red in a variable called $dsn. DSNs begin withthe characters dbi:, followed by the name of the driver. For example, the DSN for a MySQLdatabase is dbi:mysql. You can expand the code in Listing 3-1 <strong>to</strong> show the valid DSNs for thegiven database drivers on your system by using the data_sources() DBI method, as shown inListing 3-2.Listing 3-2. Listing Valid DSNs#!/usr/bin/perluse strict;use DBI;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!