perl 编码 qw/encode 解码/ 重新定义 Encode.pm
我正在查询表“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是不区分大小写的文件系统(通常在 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),
use
ing 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, therequire
part of the job will seekENCODE.pm
, load it and store it in%INC
. But when any other part of the code then attempts touse
it under its proper name (forEncode
, this happens through anEncode
->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.