解码输入,编码输出。
use open ':std', ':encoding(UTF-8)'; # Outputs are UTF-8
BEGIN { binmode STDIN; } # ...but not the raw CGI request.
use CGI qw( -utf8 ); # Decode parameters
use DBI qw( );
{
my $cgi = CGI->new();
print $cgi->header(
-type => "text/plain", # Just cause it's shorter.
-charset => "UTF-8", # Tell browser encoding used.
);
my $dbh = DBI->connect(
"dbi:SQLite:dbname=/tmp/tmp.sqlite", "", "",
{
AutoCommit => 1,
RaiseError => 1,
PrintError => 0,
PrintWarn => 1,
sqlite_unicode => 1, # Encode and decode for us.
},
);
$dbh->do("CREATE TABLE Testing ( str TEXT )");
my $from_html_parser = "\x{2122}";
# Should be 2122, since the trademark symbol is U+2122.
printf("from_html_parser = %v04X\n", $from_html_parser);
print("$from_html_parser\n");
$dbh->do("INSERT INTO Testing VALUES (?)", undef, $from_html_parser);
my $from_database = $dbh->selectrow_array("SELECT * FROM Testing");
# Should be 2122, since the trademark symbol is U+2122.
printf("from_database = %v04X\n", $from_database);
print("$from_database\n");
}
END { unlink("/tmp/tmp.sqlite"); }