Using GDBM with PERL

Posted on 2003-02-22
Medium Priority
Last Modified: 2008-03-06
Hi, I want to use GDBM with Perl, I've tried searching the web over and over, and I get nothing.  How do I initialize a database? how do I store in it? how do i retrieve from it? Any help would be greatly appreciated.
Question by:ravaneli
  • 3

Expert Comment

ID: 7999387
ok.  GDBM isn't standard to most perl installs.  and the web is apparently out to hide perl GDBM from users.  try this, it worked for me, even if slightly roundabout:


find "GDBM_File" on the page.  you should see six files.


download all of these into a new folder.  notice there is no README.  doesn't matter though, most perl module installations are the same process:

perl Makefile.PL   (creates "Makefile")

if you're not "root" or "admin", then you probably want to install it to a local working directory, like your homedir.  edit the Makefile, changing the PREFIX and following INSTALL*  variables to be a local directory like /home/me/lib/.  else, it will know where to put them by default

make test
make install

this will get things installed.  use the following line to do the most basic test.

perl -e 'use GDBM_File;'

if this runs smoothly without error, then you're doing well.

Expert Comment

ID: 7999404
how to use GDBM_File.  the perldoc documentation is quite short.  perl GDBM is supposed to mimic most of the traditional GNU gdbm behavior.  i've only see it accomplish this through tied variable use.  which is simple enough.

use GDBM_File;
tie %hash, 'GDBM_File', $filename, &GDBM_WRCREAT, 0640;

# using the hash array, anything you store in the hash will be put in the tied GDBM_File db
# thus it is persistant, even after the perl script terminates

$hash{"key"} = "value";
print $hash{"key2_that_i_stored_yesterday"},"\n";

untie %hash;


Author Comment

ID: 8003708
ok... I understand that a hash only has a value and a key for a every element.  But what if I want to store a third entry for the elements within the database.  How do I do that?

So if I have an essay for example, and want to store every word in it, how many times it occurs, and the line at which it occurs in the essay, how do i do that?

Accepted Solution

blinkie23 earned 400 total points
ID: 8003880
normally in perl you can do multi-dimensional hashes or arrays.

$hash{key}{field1} = "this1";
$hash{key}{field2} = "this2";
$hash{key}{field3} = "this3";

if i remember correctly, the GBM libraries don't allow for multi-dimensional hash access via a tied variable to the db.  nor do they allow much in terms of references as values.  i remember this being an annoying problem.  first, test it out on your GDBM hash, as i've not worked with the gdbm variation, only with DB_File.

but, you are always able to do other creative data storage methods if you only have persistant access to a key/value pair for storage.

For instance, use "::" to delimit fields in your value.

$hash{key} = "this1::this2::this3";

a simple join or split is all that is needed to convert to this.  a small loop will fill or extract from a multi-dimensional hash/array.

similarly, you can try using the Freeze/Thaw methods of Perl, which supposively can turn (freeze) any type of datastructure into an ascii string representation (garble to you and i).  you store that, and simply thaw it out when you extract the value.  this may take some exploring though, check the web for documentations.  i believe it is:

use Storable qw(freeze thaw);

good luck.
LVL 20

Expert Comment

ID: 9692029
Nothing has happened on this question in over 8 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by blinkie23.

Please post any comments here within the next seven days. Moderators check comments here before acting on the recommendation. Experts: silence will likely be taken as assent.


EE Cleanup Volunteer

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question