我正在尝试将使用 BerkeleyDB 的绑定哈希传递给子例程并在例程中修改数据库的内容,但它不起作用。
#!/usr/bin/perl
use warnings;
use strict;
use BerkeleyDB;
sub testdb($)
{
my $dbptr = shift;
my %db = %{$dbptr};
print("inside foo: ", $db{'foo'}, "\n");
$db{'biz'} = "baz";
return 0;
}
my %database;
my $dbhand = tie %database, 'BerkeleyDB::Hash', -Filename => "test.db";
print "outside foo: ", $database{'foo'}, "\n";
testdb(\%database);
print "returned: ", $database{'biz'}, "\n";
$dbhand->db_close();
undef($dbhand);
untie %database;
exit(0);
但是当我运行它时:
./dbtie
outside foo: foobar
inside foo: foobar
Use of uninitialized value in print at ./dbtie line 24.
returned:
所以看起来我可以从数据库中读取,但我不能写入它。为什么?
我尝试在子例程结束时执行 db_sync() ,但没有任何区别。
这是使用 BerkeleyDB 0.54 版的 Perl 5.14。它反过来使用 Berkeley DB 版本 6,创建版本 9 哈希表。