eciii
eciii

Reputation: 317

How to use gdbm in Perl

I'm new to gdbm and I would like to use it in Perl. I know that Perl ships by default with a module for that (GDBM_File). Now, when I try the simplest example possible, namely:

#!/usr/bin/perl

use strict;
use warnings;

use GDBM_File;

my $dbfile = '/tmp/test.gdbm';

my $ok = tie(my %db, 'GDBM_File', $dbfile, &GDBM_WRCREAT, 0664);
die "can't tie to $dbfile for WRCREAT access: $!" unless $ok;

$db{test} = 1;

untie %db;

and execute it I get the following warning:

untie attempted while 1 inner references still exist at ./gdbm-test line 13.

I read the perl documentation (see the "untie gotcha" in the provided link) but that explanation does not seem to apply here since it is clear that %db has no references anywhere in the code pointing to it.

Nonetheless the code seems to work since when I inspect the database file I get the correct result:

bash$ echo list | gdbmtool /tmp/test.gdbm 
test 1

Why does this warning appear and how can I get rid of it?

Upvotes: 0

Views: 448

Answers (1)

Dave Cross
Dave Cross

Reputation: 69284

I think that this is, in fact, a manifestation of the gotcha that you point to. The documentation for tie() says this:

The object returned by the constructor is also returned by the tie function

So your $ok contains a reference to the object, and you should undefine that before calling untie().

undef $ok;
untie %db;

Upvotes: 3

Related Questions