Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 17.1 DBM Databases and DBM HashesChapter 17
Database Manipulation
Next: 17.3 Using a DBM Hash
 

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 format; the frequently used value of 0666 provides read-write access to the database. If the files already exist, this parameter has no effect. For example:

dbmopen(%FRED, "mydatabase", 0666); # 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 read/write attributes.

The return value from 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,"c:/temp/xx",undef) || die "cannot open DBM c:/temp/xx";

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

The DBM array stays open throughout the program. When the program terminates, the association is terminated. You can also break the association in a manner similar to closing a filehandle, by using the dbmclose function:

dbmclose(%A);

Like close, dbmclose returns false if something goes wrong.


Previous: 17.1 DBM Databases and DBM HashesLearning Perl on Win32 SystemsNext: 17.3 Using a DBM Hash
17.1 DBM Databases and DBM HashesBook Index17.3 Using a DBM Hash



Banner.Novgorod.Ru