ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
11.14. Transparently Persistent Data StructuresProblemYou have a complex data structure that you want to persist outside your program. SolutionUse 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; DiscussionA 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 # this doesn't work! $hash{"some key"}[4] = "fred"; # RIGHT $aref = $hash{"some key"}; $aref->[4] = "fred"; $hash{"some key"} = $aref; See AlsoThe documentation for the CPAN module MLDBM; Recipe 14.1; Recipe 14.7; Recipe 14.11 |