Feb 042013
Article Perl

In our previous post “Introduction to Berkeley DB” we presented this database management library, and commented about the characteristics and functionality available from it. This post contains practical examples of how to work with Berkeley DB from a perl script.


Berkeley DB is implemented as a system library that should be installed using the package management procedure suitable for the operating system in use. For instance, in many Linux distributions this can be done by means of the “apt-get” command:

Besides, to work in perl with BDB, the BerkeleyDB CPAN module should be installed as well:

Create/Open a database

The example code below opens a BDB of type Hash contained in a file named “/home/data.db”. By specifying the DB_CREATE flag, the database is created if it did not previously exist:

Adding records

A record in Berkeley DB is a pair (key,value). A record is added by calling the db_put method of the $dbh database handle:


Process all the records in the database

The content of the database can be traversed by means of a cursor:

Check if a key exists, and retrieve the associated value

The value for a given key can be retrieved using the db_get method of the database handle:

As we can see in the example above, the value retrieved is stored in the $value variable. If the key does not exist or some other error happens, the db_get method returns an error message.

If we just need to check the existence of the key, but do not need to retrieve the value, we can use the db_exists method instead:

Storing data structures as values in BDB

Often, we might be interested in storing complex data structures: arrays, hashtables,… whose elements can be simple values, of references to other data structures. To do this, we need to serialize the data structure: convert it to a string that can be stored in the database, and can be later converted back into the original data structure using a deserialization procedure.

There are several perl modules available to perform this serialization/deserialization process. One of the most popular is JSON::XS. The next example shows how to use this module:

To retrieve the original structure, we perform the inverse operation:

Besides, if the values to be stored in the database are large (above 1 KB in size), and the volume of data in the database is big, we might be interested in compressing the values to save disk space. This can be easily done with the IO::Compress::Deflate and IO::Uncompress::Inflate modules.

 Posted by at 1:33 pm

 Leave a Reply