1

I am using a DBM::Deep hash object like so:

my $dbm = DBM::Deep->new(
        file      => "dbm.db",
        locking   => 1,
        autoflush => 1,
        type      => "DBM::Deep->TYPE_HASH",
        );
#code..
$dbm = $hash_reference;

However, this doesn't work. $dbm holds the correct values during the program, but after it exits dbm.db is empty and when I start up another program that tries to use dbm.db, there's nothing in it. But whenever I copy the hash reference like this (it's a two level deep hash):

    for my $id (keys %$hash_reference) {
        for(keys %{$hash_reference->{$id}}) {
            $todo->{$id}->{$_} = $hash_reference->{$id}->{$_};
        }
    }

Then it will copy everything over correctly and the values will still be there after program execution. The DBM author seems to stress though that his DBM::Deep objects work just like a regular hash, so does anyone know if there is an easier way to do this? Thanks!

srchulo
  • 5,143
  • 4
  • 43
  • 72

1 Answers1

5

You’re throwing away the object. Try this instead:

%$dbm = %$hash_reference;
tchrist
  • 78,834
  • 30
  • 123
  • 180
  • Do you mean I'm throwing away the DBM object because after I assign it it's just a regular perl hash reference? – srchulo Aug 04 '12 at 00:25
  • Yep, replace the hash inside the tied reference rather than replace the tied reference with a plain reference. – zostay Aug 04 '12 at 02:31