开发者

perl Encode qw/encode decode/ redefined Encode.pm

开发者 https://www.devze.com 2023-02-11 07:28 出处:网络
I\'m querying table \"post\"; its fields username and content are utf8 chinese. I need to convert them to b开发者_开发问答ig5 to print in windows console. My script fails to compile, reporting errors

I'm querying table "post"; its fields username and content are utf8 chinese. I need to convert them to b开发者_开发问答ig5 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
...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消