Question

Reading about GDBM in this book they only give simple examples of the data structure that can stored. E.g.

$dbm{'key'} = "value";

Background

I would like to save many small text files in the database for local use only, and use nested hashes and arrays to represent the file paths. It doesn't have to be GDBM, but it seams to be the only key/value database library for Perl.

Question

Can I store any hash in GDBM no matter have many nested hashes and arrays it contains?

Does GDBM offer any search features, or I am left to implement my own in Perl?

Was it helpful?

Solution

DBM databases don't support arrays at all. They are esssentially the same as a Perl hash, except that the value of an item can only be a simple string and may not be a number or a reference. The keys and values for each data item in a DBM database are simple byte sequences. That is, the API represents them by a char pointer and an int size.

Within that constraint you can use the database however you like, but remember that, unlike SQL databases, every key must be unique.

You could emulate nested hashes by using the data fetched by one access as a key for the next access but, bearing in mind the requirement for unique keys, that's far from ideal.

Alternatively, the value fetched could be the name of another DBM database which you could go on to query further.

A final option is to concatenate all the keys into a single value, so that

$dbm{aa}{bb}{cc}

would actually be implemented as something like

$dbm{aa_bb_cc}

OTHER TIPS

Actually, you can store hashes of hashes, or lists of lists in perl. You use the MLDBM module from CPAN, along with the dbm of your choice..

check out this online pdf book and go to chapter 13. [https://www.perl.org/books/beginning-perl/][1]

The complex part is figuring out how to access the various levels of references. To search you would have to run through the keys and parse the values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top