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.2. Opening and Closing DBM Hashes

To associate a DBM database with a DBM array, use the dbmopen function, which looks like this:

dbmopen(%ARRAYNAME, "dbmfilename", $mode);

The %ARRAYNAME parameter is a Perl hash. (If this hash already has values, the values are discarded.) This hash becomes connected to the DBM database called dbmfilename, usually stored on disk as a pair of files called dbmfilename.dir and dbmfilename.pag.

The $mode parameter is a number that controls the permission bits of the pair of files if the files need to be created. The number is typically specified in octal: the frequently used value of 0644 gives read-only permission to everyone but the owner, who gets read-write permission. If the files already exist, this parameter has no effect. For example:

dbmopen(%FRED, "mydatabase", 0644); # open %FRED onto mydatabase

This invocation associates the hash %FRED with the disk files mydatabase.dir and mydatabase.pag in the current directory. If the files don't already exist, they are created with a mode of 0644 modified by the current umask.

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

dbmopen(%A,"/etc/xx",undef) || die "cannot open DBM /etc/xx";

In this case, if the files /etc/xx.dir and /etc/xx.pag cannot be opened, the dbmopen call returns false, rather than attempting to create the files. ...

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