Going Off the Beaten DB Path (cont'd)
Won't Break a Sweat Building a Hash
You can go a long way simply treating the Berkeley DB file as hash frozen in space, using only Perl's built-in functions to deal with it. The advantage over a standard Perl hash, of course, is that the Berkeley DB file is stored on disk instead of being held in memory, so it can be much larger. If you tried to build a hash with 10 million rows, for example, you'd run out of memory pretty quickly, but a Berkeley DB file could handle it without breaking a sweat.
In addition to Perl's built-in functions, there's a set of Berkeley DB API methods we can use to manipulate data and get results. To use those methods, you need to get the return value from the tie function and work with it.
$obj = tie %hash, "DB_File", "myDB";
# add a key/value pair
$obj->put("key4", "value4");
# delete a key/value pair
$obj->del("key3", "value3");
# find a value from a given key, assign it to $value
$obj->get("key2", $value);
The DB_BTREE file format stores keys in alphabetical order by default, but it's possible to alter the sort order if it suits your purposes. DB_BTREE can also handle duplicate key entries, by setting the R_DUP flag:
$DB_BTREE->{'flags'} = R_DUP;