Lightweight
Persistent Data
Randal L. Schwartz
Frequently, you have data with a strong will to live. That is, your data must
persist between invocations of your program and occasionally even be shared
between simultaneous invocations.
At the high end of this demand, we have entire companies devoted to creating
high-performance, multi-user, SQL-interfaced databases. These databases are
usually accessed from Perl via the DBI package, or by some wrapper slightly
above DBI, such as Class::DBI or DBIx::SQLEngine. The details
of SQL might even be entirely hidden away using a higher level package like
Tangram or Alzabo.
But further down the scale, there are some new solutions popping onto the
scene, which invite further observation, as well as some old classic solutions.
For example, since Perl version 2 we've been able to put a hash out on disk
with dbmopen:
dbmopen(%HASH, "/path/on/disk", 0644) || die;
$HASH{"key"} = "value";
dbmclose(%HASH);
The effect of such code is that we now have a key/value pair stored in an external
structured file. We can later come along and reopen the database as a hash again,
and treat it as if it were a hash with preexisting values:
dbmopen(%HASH, "/path/on/disk", 0644) || die;
foreach $key (sort keys %HASH) {
print "$key => $HASH{$key}\n";
}
dbmclose(%HASH);
While the interface was relatively simple, I wrote quite a few programs before
Perl5 came around using this storage mechanism for my persistence. However, this
storage suffered some limitations: the keys and values had to be under a given
size, access to the structure could not handle multi-user reads and writes, and
the resulting data files were not necessarily portable to other machines (because
they used incompatible libraries or byte orders).
|