0

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?

eciii
  • 277
  • 3
  • 8
  • This is just a suggestion, please check out [SQLite](https://www.sqlite.org/docs.html) which might be better option although you did not stated what you try to achieve. Perl [DBI module](https://metacpan.org/pod/DBD::SQLite), [Tutorial](https://www.tutorialspoint.com/sqlite/sqlite_perl.htm), [Code samples](https://code-maven.com/slides/perl-programming/database-using-dbi). There is nothing wrong with [gdbm](https://www.gnu.org.ua/software/gdbm/manual.html) it is perfect fit for small projects. – Polar Bear Mar 04 '20 at 21:58
  • But if you want at some point _migrate_ to [MariaDB](https://mariadb.org/),[MySQL](https://www.mysql.com/), [PostgreSQL](https://www.postgresql.org/) -- [SQLite](https://www.sqlite.org/docs.html) will _open the door_ to more powerful DBs and you will learn common [DBI](https://metacpan.org/pod/DBI) interface. – Polar Bear Mar 04 '20 at 22:01

1 Answers1

3

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;
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • You are completely right. That is such a simple explanation that I missed from the documentation. Many thanks for your help! – eciii Mar 04 '20 at 14:22
  • @eciii: The other option (and the one I'd take) would be to not use `%ok` at all. – Dave Cross Mar 04 '20 at 14:54