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) HerongYang.com. All Rights Reserved.
#
   ($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:

herong> dbmInsert.pl dictionary

herong> 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) HerongYang.com. All Rights Reserved.
#
   ($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:

herong> 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) HerongYang.com. All Rights Reserved.
#
   ($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:

herong> dbmUpdate.pl dictionary

herong> 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) HerongYang.com. All Rights Reserved.
#
   ($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:

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

herong> dbmlookup.pl dictionary milk
milk = lait

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

Using DBM Database Files

 dbmopen() - Opening DBM Files with Hash Variables

DBM Database Example - English French Dictionary

 DBM Database Example - Book Records with Multiple Fields

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB