Transparently Persistent Data Structures (Perl Cookbook, 2nd Edition)

## 11.14. Transparently Persistent Data Structures

### 11.14.1. Problem

You have a complex data structure that you want to persist outside your program.

### 11.14.2. Solution

Use MLDBM and either (preferably) DB_File or else GDBM_File:

>     use MLDBM qw(DB_File);
>     use Fcntl;

>     tie(%hash, "MLDBM", "testfile.db", O_CREAT|O_RDWR, 0666)
>         or die "can't open tie to testfile.db: $!";

>     # ... act on %hash

>     untie %hash;

### 11.14.3. Discussion

A hash with 100,000 items in it would undoubtably take considerable time to build. Storing this to disk, either slowly by hand or quickly with Storable, is still an expensive operation in memory and computation.

The DBM modules solve this by tying hashes to disk database files. Rather than reading the whole structure in at once, they only pull in what they need, when they need it. To the user, it looks like a hash that persists across program invocations.

Unfortunately, the values in this persistent hash must be plain strings. You cannot readily use a database file as a backing store for a hash of hashes, a hash of arrays, and so on—just for a hash of strings.

However, the MLDBM module from CPAN allows you to store references in a database. It uses Data::Dumper to stringify these references for external storage:

>     use MLDBM qw(DB_File);
>     use Fcntl;
>     tie(%hash, "MLDBM", "testfile.db", O_CREAT|O_RDWR, 0666)
>         or die "can't open tie to testfile.db: $!";

Now you can use `%hash` to fetch or store complex records from disk. The only drawback is that you can't access the references piecemeal. You have to pull in the reference from the database, work with it, and then store it back.

>     # this doesn't work!
>     $hash{"some key"}[4] = "fred";

>     # RIGHT
>     $aref = $hash{"some key"};
>     $aref->[4] = "fred";
>     $hash{"some key"} = $aref;

### 11.14.4. See Also

Recipe 11.13

Copyright © 2003 O'Reilly & Associates. All rights reserved.