perl 编码 qw/encode 解码/ 重新定义 Encode.pm

发布于 2024-10-17 13:40:07 字数 771 浏览 5 评论 0原文

我正在查询表“post”;它的字段用户名和内容都是utf8中文。我需要将它们转换为 big5 以在 Windows 控制台中打印。我的脚本无法编译,报告 ENCODE 例程被重新定义的错误。

我有另一个脚本来测试没有 DBI 的编码/解码,并且它工作得很好。我该如何修复它?

脚本:

use DBI;
use strict;
use ENCODE qw /encode decode/;

my $dbh = DBI->connect("dbi:SQLite:dbname=tweetylicious.db","","",{sqlite_unicode => 1});
$dbh->do("PRAGMA foreign_keys = ON");

my $result_aref = $dbh->selectall_arrayref("SELECT * FROM post");

foreach my $user ( @$result_aref ) {
    my $name = ${$user}[1];
    my $content = ${$user}[2];
    print encode("utf8", $name), " : ",$content, "\n";
}

错误:

subroutine DEBUG redefined at path-to-lib/ENCODE.pm line 144
subroutine encoding redefined at path-to-lib/ENCODE.pm line 164
...

I'm querying table "post"; its fields username and content are utf8 chinese. I need to convert them to big5 to print in windows console. My script fails to compile, reporting errors that the ENCODE routines are redefined.

I have another script to test encode/decode without DBI, and it works fine. How can I fix it?

The script:

use DBI;
use strict;
use ENCODE qw /encode decode/;

my $dbh = DBI->connect("dbi:SQLite:dbname=tweetylicious.db","","",{sqlite_unicode => 1});
$dbh->do("PRAGMA foreign_keys = ON");

my $result_aref = $dbh->selectall_arrayref("SELECT * FROM post");

foreach my $user ( @$result_aref ) {
    my $name = ${$user}[1];
    my $content = ${$user}[2];
    print encode("utf8", $name), " : ",$content, "\n";
}

The errors:

subroutine DEBUG redefined at path-to-lib/ENCODE.pm line 144
subroutine encoding redefined at path-to-lib/ENCODE.pm line 164
...

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

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

发布评论

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

评论(1

土豪 2024-10-24 13:40:08

如果您使用的是不区分大小写的文件系统(通常在 Windows 上),使用大小写错误的模块可能会导致出现此类消息。

简短回答:use Encode (...)(注意大小写)

较长回答:Perl 区分大小写。当您使用使用错误大小写的模块时,< code>require 作业的一部分将查找 ENCODE.pm,加载它并将其存储在 %INC 中。但是,当代码的任何其他部分尝试以其正确的名称使用它时(对于Encode,这是通过Encode->发生的) Encode::Alias->Encode 循环),它不会在 %INC 中找到它,将再次加载它,然后触发所有这些重新定义消息。

使用预期的模块名称可以避免这种情况。

If you're on a case-insensitive filesystem (typically, on Windows), useing a module with the wrong case can lead to such messages.

Short answer: use Encode (...) (note the capitalization)

Longer answer: Perl is case-sensitive. When you use a module using the wrong capitalization, the require part of the job will seek ENCODE.pm, load it and store it in %INC. But when any other part of the code then attempts to use it under its proper name (for Encode, this happens through an Encode->Encode::Alias->Encode loop), it won't find it in %INC, will load it again, and that triggers all those redefinition messages.

Using the intended module name avoids this.

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