简体   繁体   English

如何在 Perl 中使用 gdbm

[英]How to use gdbm in Perl

I'm new to gdbm and I would like to use it in Perl.我是 gdbm 的新手,我想在 Perl 中使用它。 I know that Perl ships by default with a module for that (GDBM_File).我知道 Perl 默认带有一个模块(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.我阅读了perl 文档(请参阅提供的链接中的“解开陷阱”),但该解释似乎不适用于此处,因为很明显%db在代码中的任何地方都没有指向它的引用。

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?为什么会出现此警告,我该如何摆脱它?

I think that this is, in fact, a manifestation of the gotcha that you point to.我认为这实际上是您指出的问题的一种表现。 The documentation for tie() says this: tie()文档是这样说的:

The object returned by the constructor is also returned by the tie function构造函数返回的对象也是tie函数返回的

So your $ok contains a reference to the object, and you should undefine that before calling untie() .所以你的$ok包含对对象的引用,你应该在调用untie()之前untie()定义它。

undef $ok;
untie %db;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM