当我插入 Berkeley DB 时,为什么会收到来自 Perl 的宽字符警告?

发布于 2024-09-01 03:04:49 字数 90 浏览 8 评论 0原文

我正在 Berkeley DB 上进行实验。我只是从 DB a 中删除内容并将键值对重新插入到 DB b 中。但是,将键值对插入此数据库 b 时出现宽字符错误。帮助?

I'm running an experiment on Berkeley DBs. I'm simply removing the contents from DB a and reinserting the key-value pairs into DB b. However, I am getting Wide character errors when inserting key-value pairs into this DB b. Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2024-09-08 03:04:49

BerkeleyDB 存储字节(“八位字节”)。 Perl 字符串由 Perl 字符组成。为了将 Perl 字符存储在基于八位字节的存储中,您必须将字符转换为字节。这称为编码,就像字符编码一样。

您收到的警告表明 Perl 正在为您进行转换,并猜测您要使用的字符编码。因为它可能会猜错,所以最好明确地说出来。编码模块允许您做到这一点。

不要写:

$db->store( key => $value );

你应该写:

use Encode qw(encode);

$db->store( key => encode('utf-8', $value) );

并且在出路时:

use Encode qw(decode);

$db->get($key, $octets); # BDB returns the result via the arg list.  C programmers...
my $value = decode('utf-8', $octets);

这不仅适用于 BDB;还适用于 BDB。每当您通过网络、文件、终端或几乎任何其他方式进行通信时,您必须确保在传出时将字符编码为八位字节,并在传入时将八位位组解码为字符。否则,您的程序将不会工作。

BerkeleyDB stores bytes ("octets"). Perl strings are made of Perl characters. In order to store Perl characters in the octet-based store, you have to convert the characters to bytes. This is called encoding, as in character-encoding.

The warning you get indicates that Perl is doing the conversion for you, and is guessing about what character encoding you want to use. Since it will probably guess wrong, it's best to explicitly say. The Encode module allows you to do that.

Instead of writing:

$db->store( key => $value );

You should instead write:

use Encode qw(encode);

$db->store( key => encode('utf-8', $value) );

And on the way out:

use Encode qw(decode);

$db->get($key, $octets); # BDB returns the result via the arg list.  C programmers...
my $value = decode('utf-8', $octets);

This is true of more than just BDB; whenever you are communicating across the network, via files, via the terminal, or pretty much anything, you must be sure to encode characters to octets on the way out, and decode octets to characters on the way in. Otherwise, your program will not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文