O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

17.1. DBM Databases and DBM Hashes

Most UNIX systems have a standard library called DBM. This library provides a simple database management facility that allows programs to store a collection of key-value pairs into a pair of disk files. These files retain the values in the database between invocations of the programs using the database, and these programs can add new values, update existing values, or delete old values.

The DBM library is fairly simple, but being readily available, some system programs have used it for their fairly modest needs. For example, sendmail (and its variants and derivatives) stores the aliases database (the mapping of mail addresses to recipients) as a DBM database. The most popular Usenet news software uses a DBM database to track current and recently seen articles. The Sun NIS (née YP) database masters are also kept in DBM format.

Perl provides access to this same DBM mechanism through a rather clever means: a hash may be associated 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. Creating a new element in the array modifies the DBM database immediately. Deleting an element deletes the value from the DBM database. And so on.[1]

[1] This is actually just a special use of the general tie mechanism. If you want something more flexible, check out the AnyDBM_File (3), DB_File (3), and perltie (1) manpages.

The size, number, and kind of keys and values in ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required