#!/usr/local/bin/perl # # DBM Perl Script Test Program... # # Author: Matthew W. Coan # Date: Sat Apr 14 19:26:04 EDT 2012 # use warnings; use strict; use DB_File; print "running dbm test...\n"; my $file_name = "./test.db"; my $cmd = "read"; my %map; my $k; my $v; if(($#ARGV+1) == 1) { $cmd = $ARGV[0]; } if($cmd eq "write") { print "write next...\n"; unlink $file_name; tie %map,"DB_File",$file_name,O_RDWR|O_CREAT,0666,$DB_BTREE || die("unable to open: $file_name for writing...\n"); $map{"zero"}="0"; $map{"one"}="1"; $map{"two"}="2"; print "Please enter a key: "; $k = ; chomp $k; print "Please enter a vlaue: "; $v = ; chomp $v; $map{$k}=$v; print "Please enter a key to delete: "; $k = ; chomp $k; delete $map{$k}; untie %map; print "did dbm write...\n"; } else { print "read next...\n"; tie %map,"DB_File",$file_name,O_RDWR|O_CREAT,0666,$DB_BTREE || die("unable to open: $file_name for reading...\n"); print "lookup...\n"; print "zero=$map{'zero'}\n"; print "one=$map{'one'}\n"; print "two=$map{'two'}\n"; print "iterate...\n"; while(($k, $v) = each %map) { print "[$k -> $v]\n"; } untie %map; print "did dbm read...\n"; } exit;