10.1.1 Using DBM Files: dbmopen and dbmclose
10.1.1 Using DBM Files: dbmopen and dbmclose
The simplest way to use a DBM file is to use the dbmopen and dbmclose functions. To associate a DBM database with a hash, we use dbmopen. The syntax is given below.
dbmopen (%hash, dbmfilename, permissionmask)
Here, %hash is a Perl hash. dbmfilename is the name of a DBM file or database to which the hash is linked. The DBM file(s) may exist from before. If not, they are created by the call to dbmopen with the permission mask specified. The permission mask is a number given following the standard Unix directives. Using undef as the permission mask prevents Perl from creating the file if it does not exist. The following program illustrates the use of a DBM file.
Program 10.1
#!/usr/bin/perl
#Stores keys and values for customers in a DBM database
#file dbm1.pl
#Open the `customers' file and let the entries
#be available in the %CUSTOMERS hash table.
#If the file is not there, create
dbmopen (%CUSTOMERS, "customers", 0644);
print "Hash %CUSTOMERS linked to DBM file customers\n";
%CUSTOMERS = (
"719-262-3432-1" =>
"Larsen::719-262-3432::larsen\@brain.uccs.edu"
);
$CUSTOMERS {"719-574-3656-1"} =
"Kalita::719-574-3656::kalita\@pikespeak.uccs.edu";
while (($key, $value) = each %CUSTOMERS){
print "Customer ID = $key; Value = $value\n";
}
dbmclose(%CUSTOMERS);
print "Hash %CUSTOMERS de-linked from DBM file customers\n";
#********************
print "Printing contents of DBM file customers\n";
dbmopen (%CUSTOMERS, "customers",0644)
|| die "Cannot open customers database.\n";
while (($key, $value) = each %CUSTOMERS){
print "Customer ID = $key; Value = $value\n";
}
dbmclose(%CUSTOMERS);
In this program, we have a hash table or associative array called %CUSTOMERS. The program starts by linking %CUSTOMERS to the DBM database file called customers. If the customers DBM file does not exist, it is created with permission mask of 0644.
Initially, the hash has only one element. This element has the key 719-262-3432 and the corresponding value is Larsen::719-262-3432::larsen@brain.uccs.edu. The value is information about an individual. When the hash has been assigned a value, the value is immediately written out to the DBM file. We add a second element to the hash. Following this, the link between the hash and the DBM files are severed by making the call
dbmclose(%CUSTOMERS);
Any updates to the hash after the connection has been cut off are not reflected in the disk-based DBM file. However, there is nothing wrong in adding to, deleting from or otherwise updating the contents of the hash when it is no longer attached to a file.
To check that the hash has been written to a file, we link a hash to the same file again. Although the name of the hash, %CUSTOMERS, is the same as the one used previously, the new name could have been different. After the hash has been linked to the DBM file, once again, its contents become available to the program. In this part of the program, we simply print the contents to the terminal. The output of the program is given below. Lines have been broken where necessary to fit the printed page.
Hash %CUSTOMERS linked to DBM file customers
Customer ID = 719-262-3432-1;
Value = Larsen::719-262-3432::larsen@brain.uccs.edu
Customer ID = 719-574-3656-1;
Value = Kalita::719-574-3656::kalita@pikespeak.uccs.edu
Hash %CUSTOMERS de-linked from DBM file customers
Printing contents of DBM file customers
Customer ID = 719-262-3432-1;
Value = Larsen::719-262-3432::larsen@brain.uccs.edu
Customer ID = 719-574-3656-1;
Value = Kalita::719-574-3656::kalita@pikespeak.uccs.edu
The output clearly shows that the program links to the hash two times; the first time it connects, it sets a couple of key-value pairs, and disconnects. When it links to the hash a second time, it simply prints out the contents of the hash.
