Question

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
...
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top