Christiansen & Torkington in the
Perl Cookbook, Chapter 14, recipe 3
- "Converting between DBM files", show something similar to your code.
The relevant portion of that code for your problem is:
# open the files
tie(%db_in, 'DB_File', $infile)
or die "Can't tie $infile: $!";
tie(%db_out, 'GDBM_File', $outfile, GDBM_WRCREAT, 0666)
or die "Can't tie $outfile: $!";
# copy (don't use %db_out = %db_in because it's slow on big databases)
while (my($k, $v) = each %db_in) {
$db_out{$k} = $v;
}
I think you may be overcomplicating the text-file creation part. Why not just modify the
Cookbook code above a bit with something like this snippet:
# open the files
tie(%db_in, 'DB_File', $infile)
or die "Can't tie $infile: $!";
open TEXT_OUT, ">$outfile"
or die "Can't open $outfile: $!";
while (my($key, $value) = each %db_in) {
print TEXT_OUT "$key\n$value\n";
}
This way you need not slurp the whole file in (may not be scalable if
the original file is large), and after moving the text file to the new system
you need only reverse the process,
making each odd row the key and the next row the new DBM value of the new file.
Even if $value happened to
be undef, that state is still preserved.
I only ask for information. -- Charles Dickens
David Copperfield