Perl Tutorials - Herong's Tutorial Examples - v6.02, by Herong Yang
DBM Database Example - Book Records with Multiple Fields
This section provides a tutorial example of using DBM database files to build book records with multiple fields by joining them into a single string value.
This DBM example will show you how to manage a table with multiple columns. In the following Perl program, BookInsert.pl, I joined all columns into a single string with "\t" as the delimiter, and stored it into the hash:
#- BookInsert.pl #- Copyright (c) HerongYang.com. All Rights Reserved. # ($name) = @ARGV; die "Missing DBM name.\n" unless $name; dbmopen(%map,$name,0666); $id = &lastKey(); $id++; $key = $id; $title = "Programming Perl"; $author = "Larry Wall"; $date = "Jan 1991"; $isbn = "0937175641"; $edition = "1"; $val = join("\t",$title,$author,$date,$edition,$isbn); $map{$key} = $val; $id++; $key = $id; $title = "Learning Perl"; $author = " Randal L. Schwartz"; $date = "Nov 1993"; $isbn = "1565920422"; $edition = "3"; $val = join("\t",$title,$author,$date,$edition,$isbn); $map{$key} = $val; dbmclose(%map); exit; sub lastKey { local $max = 0; local $key, $val; while (($key,$val)=each(%map)) { $max = $key if ($key>$max); } return $max; }
To bring back individual columns from the hash, I used the split function as shown in the following program, BookPrint.pl
#- BookPrint.pl #- Copyright (c) HerongYang.com. All Rights Reserved. # ($name) = @ARGV; die "Missing DBM name.\n" unless $name; dbmopen(%map,$name,0666); $rec = 0; while (($key,$val)=each(%map)) { $rec++; $id = $key; ($title,$author,$date,$edition,$isbn) = split("\t",$val); print "\nRecord: $rec\n"; print " id = $id\n"; print " author = $author\n"; print " date = $date\n"; print " edition = $edition\n"; print " isbn = $isbn\n"; } dbmclose(%map); exit;
Here is the output of running these programs together:
herong> BookInsert.pl bookbase herong> BookPrint.pl bookbase Record: 1 id = 1 author = Larry Wall date = Jan 1991 edition = 1 isbn = 0937175641 Record: 2 id = 2 author = Randal L. Schwartz date = Nov 1993 edition = 1 isbn = 1565920422
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
dbmopen() - Opening DBM Files with Hash Variables
DBM Database Example - English French Dictionary
►DBM Database Example - Book Records with Multiple Fields
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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