Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

DBM Database Example - English French Dictionary

This section provides a tutorial example of using DBM database files to build an English to French dictionary with insert, update, delete and print functions.

As the first example with DBM files, I wrote several simple programs to create a simple English French dictionary, to update the dictionary, and to look up word in the dictionary.

The first program, DbmInsert.pl, creates the dictionary as a DBM file, and inserts a couple of entries:

#- DbmInsert.pl
#- Copyright (c) 1996 by Dr. Herong Yang, http://www.herongyang.com/
#
   ($name) = @ARGV;
   die "Missing DBM name.\n" unless $name;
   dbmopen(%map,$name,0666);
   $map{'apple'} = 'pomme';
   $map{'bible'} = 'bible';
   $map{'cloth'} = 'tissu';
   $map{'computer'} = 'ordinateur';
   $map{'good'} = 'bon';
   $map{'milk'} = 'lait';
   $map{'price'} = 'prix';
   $map{'ten'} = 'dix';
   dbmclose(%map);
   exit;

The mask code, 0666, is an octal number representing a permission setting to allow every user for reading and writing (rw_rw_rw_). Running this program gives no problem:

>DbmInsert.pl dictionary

>dir dictionary.*

    0 dictionary.dir
1,024 dictionary.pag

The second program, DbmPrint.pl, prints the entire content of the dictionary:

#- DbmPrint.pl
#- Copyright (c) 1996 by Dr. Herong Yang, http://www.herongyang.com/
#
   ($name) = @ARGV;
   die "Missing DBM name.\n" unless $name;
   dbmopen(%map,$name,0666);
   while (($key,$val)=each(%map)) {
      print($key, ' = ', $val, "\n");
   }
   dbmclose(%map);
   exit;

No trouble to run this program. Notice that the order of the entries is maintained as the same as they were inserted by the first program:

>DbmPrint.pl dictionary
apple = pomme
bible = bible
cloth = tissu
computer = ordinateur
good = bon
milk = lait
price = prix
ten = dix

The third program, DbmUpdate.pl, deletes an entry, and updates another entry:

#- DbmUpdate.pl
#- Copyright (c) 1996 by Dr. Herong Yang, http://www.herongyang.com/
#
   ($name) = @ARGV;
   die "Missing DBM name.\n" unless $name;
   dbmopen(%map,$name,0666);
   delete($map{'apple'});
   $map{'cloth'} = 'vetement';
   dbmclose(%map);
   exit;

Run this program, and print dictionary again. You will see that the new entry is inserted at the end of the file:

>DbmUpdate.pl dictionary

>dbmprint.pl dictionary
bible = bible
computer = ordinateur
good = bon
milk = lait
price = prix
ten = dix
cloth = vetement

The fourth program, DbmLookup.pl, looks up a word from the dictionary:

#- DbmLookup.pl
#- Copyright (c) 1996 by Dr. Herong Yang, http://www.herongyang.com/
#
   ($name, $key) = @ARGV;
   die "Missing DBM name.\n" unless $name;
   die "Missing key.\n" unless $key;
   dbmopen(%map,$name,0666);
   if ($map{$key}) {
      print($key, ' = ', $map{$key}, "\n");
   } else {
      print($key, " = (not defined)\n");
   }
   dbmclose(%map);
   exit;

Try it with a couple of words:

>dbmlookup.pl dictionary apple
apple = (not defined)

>dbmlookup.pl dictionary milk
milk = lait

Sections in This Chapter

dbmopen() - Opening DBM Files with Hash Variables

DBM Database Example - English French Dictionary

DBM Database Example - Book Records with Multiple Fields

Dr. Herong Yang, updated in 2008
DBM Database Example - English French Dictionary