Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mldbm save?

Status
Not open for further replies.

jvdboom

Programmer
Aug 18, 2002
93
0
0
BE
This would work if i didn't had an infinitive loop (it seems to save when the program is finished (not killed or crashed).
I thought actions on DBM's were directly saved to disk(and save memoryspace).
If i check the file that is created it does not contain the data.
Maybe it's not effective to continually untie & tie again a hash when somethings changed.



use MLDBM qw(DB_File Storable);
use Fcntl;
...

%users = ();
tie (%users, "MLDBM", "users.dbm",
O_CREAT|O_RDWR, 0600, $DB_File::DB_BTREE) ||
die "Could not open or create database.";


...
while (1) { #forever

...

$users{$temp{$fh}{"user"}} = $temp{$fh};

...

}

Any suggestions?
 
Your problem is that MLDBM does not support the standard multi-level perl hash structure. You have to create a temporary hash with the multi-level structure and then set the top level into your tied hash.

Something like this:

Code:
my %temp_users;

$temp_users{$temp{$fh}}{'user'} = $temp{$fh};

$users{$temp{$fh}} = $temp_users{$temp{$fh}}{'user'};

That's the basic idea, anyway. I'm not quite sure about what your example line is doing, so it may not work quite right.
 
Actually that wasn't the problem
$temp is already a tempory hash (no tie)
$users{$temp{$fh}{"user"}} = $temp{$fh};
is translated to:
$users{"foo"} = ('user' => 'foo',
'pass' => 'bar',
....
);

if you try this code:


use MLDBM qw(DB_File Storable);
use Fcntl;

%sue = ();
tie (%sue, "MLDBM", "sue1.dbm",
O_CREAT|O_RDWR, 0600, $DB_File::DB_BTREE) ||
die "Could not open or create database.";


%sue = (
'name' => 'Sue',
'age' => '45'


);

#sleep 100000000;

it will work and you will see something like this in the dbm file (last line):

 1234
45  age  1234
Sue  name


Now if you uncomment the sleep line and run it again(delete the file first) it will emulate the infinitive loop: if you check the dbm file now, there will be no lines at the end
 
it still doesn't work or I should untie/tie again it after every operation on it which could sometimes be multiple times a second.I was looking if there isn't an efficient way to do it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top