Perl and DBMs
An example of using Perl with DBMs
Here are some perl scripts. The first one just writes a small dbm with berkeley db. The second one illustrates how to get the keys from the database.

--------------------------------------------

#!/usr/local/bin/perl

#

#  This perl script writes some records to a dbm file

#

use Fcntl;

use DB_File; #Starts out and stays fast, 3sec for 20,000 records

#use NDBM_File; #Starts out OK for first 20,000, then gives error and dies

#use GDBM_File; #Starts out fast, but quickly becomes very slow

#use SDBM_File; #Failed to write anything

#use ODBM_File;



$database = 'TEMP';



tie(my %mydb, DB_File, $database, O_RDWR|O_CREAT, 0700, $DB_BTREE) ||

    die "Error:  Unable to open the Berkeley DBM database $database :$!\n";



#tie(my %mydb, NDBM_File, $database, O_RDWR|O_CREAT, 0640) ||

#    die "Error: Unable to open the NDBM database $database :$!\n";



#tie(my %mydb, GDBM_File, $database, O_RDWR|O_CREAT, 0640) ||

#    die "Error: Unable to open the GDBM database $database :$!\n";



#tie(my %mydb, SDBM_File, $database, O_RDWR|O_CREAT, 0640) ||

#    die "Error: Unable to open the SDBM database $database :$!\n";



#tie(my %mydb, ODBM_File, $database, O_RDWR|O_CREAT, 0640) ||

#    die "Error: Unable to open the ODBM database $database :$!\n";



$bigstring = "0:";

for($k=10000000;$k<10000180;$k++)

{

  $bigstring = $bigstring.$k.":";

}



$T1= time;

$k=0;

for($m=0;$m<100;$m++,$k++)

{

  if($k>19999)

  {

    $n=time-$T1;

    $size = (stat($database))[7];

    print "Record Number: $m   Time: $n  Size: $size\n";

    $k=0;

  }

  $mydb{$m} = $bigstring.$m;

}

$TD = time - $T1;



print "Time: $TD\n";



-------------------------------------------------------



#!/usr/local/bin/perl

#

# This perl script dumps the keys in a dbm file

#

use DB_File;

use Fcntl;



$database_keywords = TEMP;



tie (%GlobalKeys2Images, DB_File, $database_keywords, O_RDONLY, 0640, $DB_BTREE)

 ||

    die "Error:  Unable to open the DBM database $database_keywords :$!\n";



while(($Item, $rec) = each %GlobalKeys2Images)

{

  print "key: $Item \n";

}



untie $GlobalKeys2Images;

Media Lab Overview
LIACS Homepage
MM Conf
ACM Multimedia
ACM ICMR
IAPR ICPR
Science Direct
IEEE Library
LIACS Publications
ACM Digital Library