home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Perl in a Nutshell

Perl in a NutshellSearch this book
Previous: V. Databases Chapter 12 Next: 12.2 Design of DBI
 

12. Databases and Perl

Since one of Perl's greatest strengths is working with text, a genuine concern is how to store data. Flat files are one possibility, but don't scale very well, to say the least. Instead, you'll need to use a database.

There are two general solutions to using databases with Perl. For simple database purposes, DBM (Database Management) will serve your needs. DBM is a library supported by many (if not all) Unix systems and many non-Unix systems as well. If you use DBM with Perl, you can manipulate databases just like any hash.

For more elaborate databases with SQL interfaces, you can get a complete database product or shareware equivalent (depending on your needs) and use DBI and DBD. DBI is a module that provides a consistent interface for database solutions. A DBD is a database-specific driver that translates DBI calls as needed for that database.

In this chapter, we'll quickly cover DBM and then talk more at length about DBI/DBD.

12.1 DBM Databases and DBM Hashes

DBM is a simple database management facility for Unix systems. It allows programs to store a collection of key-value pairs in binary form, thus providing rudimentary database support for Perl. Practically all Unix systems support DBM, and for those that don't, you can get Berkeley DB from http://www.sleepycat.com/db .

To use DBM databases in Perl, you can associate a hash with a DBM database through a process similar to opening a file. This hash (called a DBM array) is then used to access and modify the DBM database. To associate a DBM database with a DBM array, you can use either the dbmopen function or the tie function with a DBM-style module. ( dbmopen is actually just a front-end to tie .) For example, with dbmopen :

dbmopen(%ARRAYNAME, "dbmfilename", $mode);
or (using tie with the DB_File module):
use DB_File;
tie(%ARRAYNAME, "DB_File", "dbmfilename");
The %ARRAYNAME parameter is a Perl hash. (If it already has values, the values are discarded.) This hash becomes connected to the DBM database called dbmfilename . This database may be stored on disk as a single file, or as two files called dbmfilename.dir and dbmfilename.pag , depending on the DBM implementation.

The $mode parameter is a number that controls the permissions of the pair of files if the files need to be created. The number is typically specified in octal. If the files already exist, this parameter has no effect. For example:

dbmopen(%BOOKS, "bookdb", 0666); # open %BOOKS onto bookdb
This invocation associates the hash %BOOKS with the disk files bookdb.dir and bookdb.pag in the current directory. If the files don't already exist, they are created with a mode of 0666, modified by the current umask .

The return value from dbmopen is true if the database could be opened or created, and false otherwise, just like the open function. If you don't want the files created, use a $mode value of undef .

Once the database is opened, anything you do to the DBM hash is immediately written to the database. See Chapter 4, The Perl Language , for more information on hashes.

dbmopen(%BOOKS, "bookdb", 0666) || die "Can't open database bookdb!";
$BOOKS{"1-56592-286-7"} = "Perl in a Nutshell";
The DBM array stays open throughout the program. When the program termi- nates, the association is terminated. You can also break the association in a manner similar to closing a filehandle, by using the dbmclose function (or untie if you used tie ). See Chapter 5, Function Reference , for more information on dbmclose , dbmopen , and tie .