/usr/share/perl5/MLDBM.pm is in libmldbm-perl 2.05-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | #
# MLDBM.pm
#
# store multi-level hash structure in single level tied hash (read DBM)
#
# Documentation at the __END__
#
# Gurusamy Sarathy <gsar@umich.edu>
# Raphael Manfredi <Raphael_Manfredi@grenoble.hp.com>
#
require 5.005;
use strict;
####################################################################
package MLDBM::Serializer; ## deferred
$MLDBM::Serializer::VERSION = $MLDBM::Serializer::VERSION = '2.05';
use Carp;
#
# The serialization interface comprises of just three methods:
# new(), serialize() and deserialize(). Only the last two are
# _required_ to be implemented by any MLDBM serialization wrapper.
#
sub new { bless {}, shift };
sub serialize { confess "deferred" };
sub deserialize { confess "deferred" };
#
# Attributes:
#
# dumpmeth:
# the preferred dumping method.
#
# removetaint:
# untainting flag; when true, data will be untainted after
# extraction from the database.
#
# key:
# the magic string used to recognize non-natively stored data.
#
# Attribute access methods:
#
# These defaults allow readonly access. Sub-class may override
# them to allow write access if any of these attributes
# makes sense for it.
#
sub DumpMeth {
my $s = shift;
confess "can't set dumpmeth with " . ref($s) if @_;
$s->_attrib('dumpmeth');
}
sub RemoveTaint {
my $s = shift;
confess "can't set untaint with " . ref($s) if @_;
$s->_attrib('removetaint');
}
sub Key {
my $s = shift;
confess "can't set key with " . ref($s) if @_;
$s->_attrib('key');
}
sub _attrib {
my ($s, $a, $v) = @_;
if (ref $s and @_ > 2) {
$s->{$a} = $v;
return $s;
}
$s->{$a};
}
####################################################################
package MLDBM;
$MLDBM::VERSION = $MLDBM::VERSION = '2.05';
require Tie::Hash;
@MLDBM::ISA = 'Tie::Hash';
use Carp;
#
# the DB package to use (we default to SDBM since it comes with perl)
# you might want to change this default to something more efficient
# like DB_File (you can always override it in the use list)
#
$MLDBM::UseDB = "SDBM_File" unless $MLDBM::UseDB;
$MLDBM::Serializer = 'Data::Dumper' unless $MLDBM::Serializer;
$MLDBM::Key = '$MlDbM' unless $MLDBM::Key;
$MLDBM::DumpMeth = "" unless $MLDBM::DumpMeth;
$MLDBM::RemoveTaint = 0 unless $MLDBM::RemoveTaint;
#
# A private way to load packages at runtime.
my $loadpack = sub {
my $pack = shift;
$pack =~ s|::|/|g;
$pack .= ".pm";
eval { require $pack };
if ($@) {
carp "MLDBM error: " .
"Please make sure $pack is a properly installed package.\n" .
"\tPerl says: \"$@\"";
return undef;
}
1;
};
#
# TIEHASH interface methods
#
sub TIEHASH {
my $c = shift;
my $s = bless {}, $c;
#
# Create the right serializer object.
my $szr = $MLDBM::Serializer;
unless (ref $szr) {
$szr = "MLDBM::Serializer::$szr" # allow convenient short names
unless $szr =~ /^MLDBM::Serializer::/;
$loadpack->($szr) or return undef;
$szr = $szr->new($MLDBM::DumpMeth,
$MLDBM::RemoveTaint,
$MLDBM::Key);
}
$s->Serializer($szr);
#
# Create the right TIEHASH object.
my $db = $MLDBM::UseDB;
unless (ref $db) {
$loadpack->($db) or return undef;
$db = $db->TIEHASH(@_)
or carp "MLDBM error: Second level tie failed, \"$!\""
and return undef;
}
$s->UseDB($db);
return $s;
}
sub FETCH {
my ($s, $k) = @_;
my $ret = $s->{DB}->FETCH($k);
$s->{SR}->deserialize($ret);
}
sub STORE {
my ($s, $k, $v) = @_;
$v = $s->{SR}->serialize($v);
$s->{DB}->STORE($k, $v);
}
sub DELETE { my $s = shift; $s->{DB}->DELETE(@_); }
sub FIRSTKEY { my $s = shift; $s->{DB}->FIRSTKEY(@_); }
sub NEXTKEY { my $s = shift; $s->{DB}->NEXTKEY(@_); }
sub EXISTS { my $s = shift; $s->{DB}->EXISTS(@_); }
sub CLEAR { my $s = shift; $s->{DB}->CLEAR(@_); }
sub new { &TIEHASH }
#
# delegate messages to the underlying DBM
#
sub AUTOLOAD {
return if $MLDBM::AUTOLOAD =~ /::DESTROY$/;
my $s = shift;
if (ref $s) { # twas a method call
my $dbname = ref($s->{DB});
# permit inheritance
$MLDBM::AUTOLOAD =~ s/^.*::([^:]+)$/$dbname\:\:$1/;
$s->{DB}->$MLDBM::AUTOLOAD(@_);
}
}
#
# delegate messages to the underlying Serializer
#
sub DumpMeth { my $s = shift; $s->{SR}->DumpMeth(@_); }
sub RemoveTaint { my $s = shift; $s->{SR}->RemoveTaint(@_); }
sub Key { my $s = shift; $s->{SR}->Key(@_); }
#
# get/set the DB object
#
sub UseDB { my $s = shift; @_ ? ($s->{DB} = shift) : $s->{DB}; }
#
# get/set the Serializer object
#
sub Serializer { my $s = shift; @_ ? ($s->{SR} = shift) : $s->{SR}; }
#
# stuff to do at 'use' time
#
sub import {
my ($pack, $dbpack, $szr, $dumpmeth, $removetaint, $key) = @_;
$MLDBM::UseDB = $dbpack if defined $dbpack and $dbpack;
$MLDBM::Serializer = $szr if defined $szr and $szr;
# undocumented, may change!
$MLDBM::DumpMeth = $dumpmeth if defined $dumpmeth;
$MLDBM::RemoveTaint = $removetaint if defined $removetaint;
$MLDBM::Key = $key if defined $key and $key;
}
# helper subroutine for tests to compare to arbitrary data structures
# for equivalency
sub _compare {
use vars qw(%compared);
local %compared;
return _cmp(@_);
}
sub _cmp {
my($a, $b) = @_;
# catch circular loops
return(1) if $compared{$a.'&*&*&*&*&*'.$b}++;
# print "$a $b\n";
# print &Data::Dumper::Dumper($a, $b);
if(ref($a) and ref($a) eq ref($b)) {
if(eval { @$a }) {
# print "HERE ".@$a." ".@$b."\n";
@$a == @$b or return 0;
# print @$a, ' ', @$b, "\n";
# print "HERE2\n";
for(0..@$a-1) {
&_cmp($a->[$_], $b->[$_]) or return 0;
}
} elsif(eval { %$a }) {
keys %$a == keys %$b or return 0;
for (keys %$a) {
&_cmp($a->{$_}, $b->{$_}) or return 0;
}
} elsif(eval { $$a }) {
&_cmp($$a, $$b) or return 0;
} else {
die("data $a $b not handled");
}
return 1;
} elsif(! ref($a) and ! ref($b)) {
return ($a eq $b);
} else {
return 0;
}
}
1;
__END__
=head1 NAME
MLDBM - store multi-level Perl hash structure in single level tied hash
=head1 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 $!;
=head1 DESCRIPTION
This module can serve as a transparent interface to any TIEHASH package
that is required to store arbitrary perl data, including nested references.
Thus, this module can be used for storing references and other arbitrary data
within DBM databases.
It works by serializing the references in the hash into a single string. In the
underlying TIEHASH package (usually a DBM database), it is this string that
gets stored. When the value is fetched again, the string is deserialized to
reconstruct the data structure into memory.
For historical and practical reasons, it requires the B<Data::Dumper> package,
available at any CPAN site. B<Data::Dumper> gives you really nice-looking dumps of
your data structures, in case you wish to look at them on the screen, and
it was the only serializing engine before version 2.00. However, as of version
2.00, you can use any of B<Data::Dumper>, B<FreezeThaw> or B<Storable> to
perform the underlying serialization, as hinted at by the L<SYNOPSIS> overview
above. Using B<Storable> is usually much faster than the other methods.
See the L<BUGS> section for important limitations.
=head2 Changing the Defaults
B<MLDBM> relies on an underlying TIEHASH implementation (usually a
DBM package), and an underlying serialization package. The respective
defaults are B<SDBM_File> and B<Data::Dumper>. Both of these defaults
can be changed. Changing the B<SDBM_File> default is strongly recommended.
See L<WARNINGS> below.
Three serialization wrappers are currently supported: B<Data::Dumper>,
B<Storable>, and B<FreezeThaw>. Additional serializers can be
supported by writing a wrapper that implements the interface required by
B<MLDBM::Serializer>. See the supported wrappers and the B<MLDBM::Serializer>
source for details.
In the following, I<$OBJ> stands for the tied object, as in:
$obj = tie %o, ....
$obj = tied %o;
=over 4
=item $MLDBM::UseDB I<or> I<$OBJ>->UseDB(I<[TIEDOBJECT]>)
The global C<$MLDBM::UseDB> can be set to default to something other than
C<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 C<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.
=item $MLDBM::Serializer I<or> I<$OBJ>->Serializer(I<[SZROBJECT]>)
The global C<$MLDBM::Serializer> can be set to the name of the serializing
package to be used. Currently can be set to one of C<Data::Dumper>,
C<Storable>, or C<FreezeThaw>. Defaults to C<Data::Dumper>. Alternatively,
you can specify the name of the serializer package at C<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.
=back
=head2 Controlling Serializer Properties
These methods are meant to supply an interface to the properties of the
underlying serializer used. Do B<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.
=over 4
=item $MLDBM::DumpMeth I<or> I<$OBJ>->DumpMeth(I<[METHNAME]>)
If the serializer provides alternative serialization methods, this
can be used to set them.
With B<Data::Dumper> (which offers a pure Perl and an XS verion
of its serializing routine), this is set to C<Dumpxs> by default if that
is supported in your installation. Otherwise, defaults to the slower
C<Dump> method.
With B<Storable>, a value of C<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 B<Storable>. Both B<Data::Dumper> and
B<FreezeThaw> are always architecture neutral.
B<FreezeThaw> does not honor this attribute.
=item $MLDBM::Key I<or> I<$OBJ>->Key(I<[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.
B<Storable> and B<FreezeThaw> do not honor this attribute.
=item $MLDBM::RemoveTaint I<or> I<$OBJ>->RemoveTaint(I<[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.
B<Data::Dumper> uses C<eval()> to deserialize and is therefore subject to
taint checks. Can be set to a true value to make the B<Data::Dumper>
serializer untaint the data retrieved. It is not enabled by default.
Use with care.
B<Storable> and B<FreezeThaw> do not honor this attribute.
=back
=head1 EXAMPLES
Here is a simple example. Note that does not depend upon the underlying
serializing package--most real life examples should not, 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
Here is another small example using 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
=head1 BUGS
=over 4
=item 1.
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.
=item 2.
The B<Data::Dumper> serializer uses eval(). A lot. Try the B<Storable>
serializer, which is generally the most efficient.
=back
=head1 WARNINGS
=over 4
=item 1.
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.
=item 2.
MLDBM does well with data structures that are not too deep and not
too wide. You also need to be careful about how many C<FETCH>es your
code actually ends up doing. Meaning, you should get the most mileage
out of a C<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 C<$mldb{foo}>, translates to a MLDBM C<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};
}
=back
=head1 AUTHORS
Gurusamy Sarathy <F<gsar@umich.edu>>.
Support for multiple serializing packages by
Raphael Manfredi <F<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.
=head1 VERSION
Version 2.05
=head1 SEE ALSO
perl(1), perltie(1), perlfunc(1), L<Data::Dumper>, L<FreezeThaw>, L<Storable>, L<DBM::Deep>, L<MLDBM::Serializer::JSON>.
=cut
|