MLDBM - store multi-level hash structure in single level tied hash
use MLDBM; # this gets the default, SDBM #use MLDBM qw(DB_File); $dbm = tie %o, MLDBM [..other DBM args..] or die $!;
This module, intended primarily for use with DBM packages, can serve as a transparent interface to any TIEHASH package that must be used to store arbitrary perl data, including nested references.
It works by converting the values in the hash that are references, to their string representation in perl syntax. When using a DBM database, it is this string that gets stored.
It requires the Data::Dumper package, available at any CPAN site.
See the BUGS section for important limitations.
You may want to set $MLDBM::UseDB to default to something other than "SDBM_File", in case you have a more efficient DBM, or if you want to use this with some other TIEHASH implementation. Alternatively, you can specify the name of the package at use time. Nested module names can be specified as "Foo::Bar".
use
Defaults to the magic string used to recognize MLDBM data. It is a six character wide, unique string. This is best left alone, unless you know what you're doing.
This controls which of the two dumping methods available from Data::Dumper are used. By default, this is set to "Dumpxs", the faster of the two methods, but only if MLDBM detects that "Dumpxs" is supported on your platform. Otherwise, defaults to the slower "Dump" method.
Data::Dumper
This can be set to a true value to make MLDBM untaint the data retrieved from the underlying DBM implementation. It is not enabled by default. Use with care.
use MLDBM; # this gets SDBM #use MLDBM qw(DB_File); use Fcntl; # to get 'em constants $dbm = tie %o, MLDBM, 'testmldbm', O_CREAT|O_RDWR, 0640 or die $!; $c = [\ 'c']; $b = {}; $a = [1, $b, $c]; $b->{a} = $a; $b->{b} = $a->[1]; $b->{c} = $a->[2]; @o{qw(a b c)} = ($a, $b, $c); # # to see what wuz stored # use Data::Dumper; print Data::Dumper->Dump([@o{qw(a b c)}], [qw(a b c)]); # # to modify data in a substructure # $tmp = $o{a}; $tmp->[0] = 'foo'; $o{a} = $tmp; # # can access the underlying DBM methods transparently # #print $dbm->fd, "\n"; # DB_File method
Adding or altering substructures to a hash value is not entirely transparent in current perl. If you want to store a reference or modify an existing reference value in the DBM, it must first be retrieved and stored in a temporary variable for further modifications. In particular, something like this will NOT work properly:
$mldb{key}{subkey}[3] = 'stuff'; # won't work
Instead, that must be written as:
$tmp = $mldb{key}; # retrieve value $tmp->{subkey}[3] = 'stuff'; $mldb{key} = $tmp; # store value
This limitation exists because the perl TIEHASH interface currently has no support for multidimensional ties.
Uses eval(). A lot.
Many DBM implementations have arbitrary limits on the size of records that can be stored. For example, SDBM and many ODBM or NDBM implementations have a default limit of 1024 bytes for the size of a record. MLDBM can easily exceed these limits when storing large data structures, leading to mysterious failures. Although SDBM_File is used by MLDBM by default, it is not a good choice if you're storing large data structures. Berkeley DB and GDBM both do not have these limits, so I recommend using either of those instead.
Gurusamy Sarathy gsar@umich.edu
Copyright (c) 1995-97 Gurusamy Sarathy. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Version 1.25 7 December 1997
perl(1), perltie(1), perlfunc(1)
To install MLDBM, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MLDBM
CPAN shell
perl -MCPAN -e shell install MLDBM
For more information on module installation, please visit the detailed CPAN module installation guide.