MLDBM(3pm)
β¨ NAME β¨
MLDBM β okay so it stores a whole multi-level Perl hash structure inside a single-level tied hash π like, nesting? handled. slay.
πΈ SYNOPSIS πΈ
use MLDBM; # this gets the default, SDBM
#use MLDBM qw(DB_File FreezeThaw); # use FreezeThaw for serializing
#use MLDBM qw(DB_File Storable); # use Storable for serializing
$dbm = tie %o, 'MLDBM' [..other DBM args..] or die $!;
π DESCRIPTION π
Bestie. This module is a transparent interface to literally any TIEHASH package that needs to store arbitrary perl data β nested references included, no cap. So yeah, you can absolutely use it to stash references and other chaotic arbitrary data inside DBM databases. π
Here's the tea on how it works: it serializes the references sitting in your hash down into a single string. Then in the underlying TIEHASH package (usually a DBM database), it's that one string that actually gets stored. And when you fetch the value again? The string gets deserialized and your beautiful data structure is reconstructed back into memory. π¦
For historical and practical reasons, it requires the Data::Dumper package, which you can grab at any CPAN site. Data::Dumper gives you really nice-looking dumps of your data structures in case you wanna stare at them on the screen β and it was the only serializing engine before version 2.00. BUT. As of version 2.00, you can use any of Data::Dumper, FreezeThaw or Storable to do the underlying serialization, exactly as hinted at in the SYNOPSIS up there. And using Storable is usually much faster than the other methods. β‘
See the BUGS section for important limitations, bestie, don't skip it.
π Changing the Defaults π
MLDBM relies on an underlying TIEHASH implementation (usually a DBM package) plus an underlying serialization package. The respective defaults are SDBM_File and Data::Dumper. Both of these defaults can be changed. And changing the SDBM_File default is strongly recommended. See WARNINGS below. π
Three serialization wrappers are currently supported: Data::Dumper, Storable, and FreezeThaw. Additional serializers can be supported by writing a wrapper that implements the interface required by MLDBM::Serializer. See the supported wrappers and the MLDBM::Serializer source for details.
In the following, $OBJ stands for the tied object, as in:
$obj = tie %o, ....
$obj = tied %o;
$MLDBM::UseDB or $OBJ->UseDB([TIEDOBJECT])
The global $MLDBM::UseDB can be set 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, as the first "parameter". Nested module names can be specified as "Foo::Bar".
The corresponding method call returns the underlying TIEHASH object when called without arguments. It can be called with any object that implements Perl's TIEHASH interface, to set that value.
$MLDBM::Serializer or $OBJ->Serializer([SZROBJECT])
The global $MLDBM::Serializer can be set to the name of the serializing package to be used. Currently can be set to one of "Data::Dumper", "Storable", or "FreezeThaw". Defaults to "Data::Dumper". Alternatively, you can specify the name of the serializer package at use time, as the second "parameter".
The corresponding method call returns the underlying MLDBM serializer object when called without arguments. It can be called with an object that implements the MLDBM serializer interface, to set that value.
π§ Controlling Serializer Properties π§
These methods are meant to supply an interface to the properties of the underlying serializer used. Do not call or set them without understanding the consequences in full. The defaults are usually sensible. π
Not all of these necessarily apply to all the supplied serializers, so we specify when to apply them. Failure to respect this will usually lead to an exception.
$MLDBM::DumpMeth or $OBJ->DumpMeth([METHNAME])
If the serializer provides alternative serialization methods, this can be used to set them.
With Data::Dumper (which offers a pure Perl and an XS version of its serializing routine), this is set to "Dumpxs" by default if that is supported in your installation. Otherwise, defaults to the slower "Dump" method.
With Storable, a value of "portable" requests that serialization be architecture neutral, i.e. the deserialization can later occur on another platform. Of course, this only makes sense if your database files are themselves architecture neutral. By default, native format is used for greater serializing speed in Storable. Both Data::Dumper and FreezeThaw are always architecture neutral.
FreezeThaw does not honor this attribute.
$MLDBM::Key or $OBJ->Key([KEYSTRING])
If the serializer only deals with part of the data (perhaps because the TIEHASH object can natively store some types of data), it may need a unique key string to recognize the data it handles. This can be used to set that string. Best left alone.
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 are doing.
Storable and FreezeThaw do not honor this attribute.
$MLDBM::RemoveTaint or $OBJ->RemoveTaint([BOOL])
If the serializer can optionally untaint any retrieved data subject to taint checks in Perl, this can be used to request that feature. Data that comes from external sources (like disk-files) must always be viewed with caution, so use this only when you are sure that that is not an issue. β οΈ
Data::Dumper uses "eval()" to deserialize and is therefore subject to taint checks. Can be set to a true value to make the Data::Dumper serializer untaint the data retrieved. It is not enabled by default. Use with care.
Storable and FreezeThaw do not honor this attribute.
π₯ EXAMPLES π₯
Here's a simple one, bestie. Note that it does not depend upon the underlying serializing package β most real life examples shouldn't, usually.
use MLDBM; # this gets SDBM and Data::Dumper
#use MLDBM qw(SDBM_File Storable); # SDBM and Storable
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 was 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
And here's another little one, this time with Storable in a portable format:
use MLDBM qw(DB_File Storable); # DB_File and Storable
tie %o, 'MLDBM', 'testmldbm', O_CREAT|O_RDWR, 0640 or die $!;
(tied %o)->DumpMeth('portable'); # Ask for portable binary
$o{'ENV'} = \%ENV; # Stores the whole environment
π BUGS π
- 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. π
- The
Data::Dumperserializer useseval(). A lot. Try theStorableserializer, which is generally the most efficient.
π¨ WARNINGS π¨
- Many DBM implementations have arbitrary limits on the size of records that can be stored. For example,
SDBMand manyODBMorNDBMimplementations have a default limit of1024bytes for the size of a record. MLDBM can easily exceed these limits when storing large data structures, leading to mysterious failures. AlthoughSDBM_Fileis used by MLDBM by default, it is not a good choice if you're storing large data structures.Berkeley DBandGDBMboth do not have these limits, so I recommend using either of those instead. π«Ά - MLDBM does well with data structures that are not too deep and not too wide. You also need to be careful about how many
"FETCH"es your code actually ends up doing. Meaning, you should get the most mileage out of a"FETCH"by holding on to the highest level value for as long as you need it. Remember that every toplevel access of the tied hash, for example$mldb{foo}, translates to a MLDBM"FETCH()"call.
Too often, people end up writing something like this:
tie %h, 'MLDBM', ...;
for my $k (keys %{$h{something}}) {
print $h{something}{$k}[0]{foo}{bar}; # FETCH _every_ time!
}
when it should be written this for efficiency:
tie %h, 'MLDBM', ...;
my $root = $h{something}; # FETCH _once_
for my $k (keys %$root) {
print $k->[0]{foo}{bar};
}
Like, seriously bestie: every toplevel access = one FETCH(). Be mindful. Your database will thank you. π
π AUTHORS π
Gurusamy Sarathy <gsar@umich.edu>.
Support for multiple serializing packages by Raphael Manfredi <Raphael_Manfredi@grenoble.hp.com>.
Test suite fixes for perl 5.8.0 done by Josh Chamas.
Copyright (c) 1995-98 Gurusamy Sarathy. All rights reserved.
Copyright (c) 1998 Raphael Manfredi.
Copyright (c) 2002 Josh Chamas, Chamas Enterprises Inc.
Copyright (c) 2010-2013 Alexandr Ciornii (alexchorny@gmail.com).
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
β VERSION β
Version 2.05 π«
π SEE ALSO π
perl(1)perltie(1)perlfunc(1)Data::DumperFreezeThawStorableDBM::DeepMLDBM::Serializer::JSON
okay that's the whole thing, bestie, no notes π this page came from User Contributed Perl Documentation β perl v5.36.0, dated 2022-10-16 β and you're officially done reading. go store some nested hashes, you icon. πβ¨
π¦ where this page comes from
- ubuntu noble: libmldbm-perl 2.05-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmldbm-perl 2.05-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmldbm-perl 2.05-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms