単一ファイルのファイルベースのブログである pplog をいじっています。
ファイルコードへの書き込み:
open(FILE, ">$config_postsDatabaseFolder/$i.$config_dbFilesExtension");
my $date = getdate($config_gmt);
print FILE $title.'"'.$content.'"'.$date.'"'.$category.'"'.$i; # 0: Title, 1: Content, 2: Date, 3: Category, 4: FileName
print 'Your post '. $title.' has been saved. <a href="?page=1">Go to Index</a>';
close FILE;
入力テキスト:
春眠不覺曉,處處聞啼鳥. 夜來風雨聲,花落知多小.
ファイルに保存すると、次のようになります。
春眠不覺�›�,處處聞啼鳥. 夜來風�›�聲,花落知多小.
Eclipse を使用してファイルを編集し、通常のレンダリングを行うことができます。ファイルへの印刷中に問題が発生しました。
基本情報: utf8 を使用しない Strawberry perl 5.12; use utf8; を試してみましたが、効果がありません。
ありがとうございました。
--- 編集 --- コメントありがとうございます。私はコードをたどった:
コードは新しいコンテンツを追加します:
# Blog Add New Entry Page
my $pass = r('pass');
#BK 7JUL09 patch from fedekun, fix post with no title that caused zero-byte message...
my $title = r('title');
my $content = '';
if($config_useHtmlOnEntries == 0)
{
$content = bbcode(r('content'));
}
else
{
$content = basic_r('content');
}
my $category = r('category');
my $isPage = r('isPage');
sub r
{
escapeHTML(param($_[0]));
}
sub r コマンドを CGI.pm 関数に転送します。
CGI.pmで
sub escapeHTML {
# hack to work around earlier hacks
push @_,$_[0] if @_==1 && $_[0] eq 'CGI';
my ($self,$toencode,$newlinestoo) = CGI::self_or_default(@_);
return undef unless defined($toencode);
$toencode =~ s{&}{&}gso;
$toencode =~ s{<}{<}gso;
$toencode =~ s{>}{>}gso;
if ($DTD_PUBLIC_IDENTIFIER =~ /[^X]HTML 3\.2/i) {
# $quot; was accidentally omitted from the HTML 3.2 DTD -- see
# <http://validator.w3.org/docs/errors.html#bad-entity> /
# <http://lists.w3.org/Archives/Public/www-html/1997Mar/0003.html>.
$toencode =~ s{"}{"}gso;
}
else {
$toencode =~ s{"}{"}gso;
}
# Handle bug in some browsers with Latin charsets
if ($self->{'.charset'}
&& (uc($self->{'.charset'}) eq 'ISO-8859-1' # This line cause trouble. it treats Chinese chars as ISO-8859-1
|| uc($self->{'.charset'}) eq 'WINDOWS-1252')) {
$toencode =~ s{'}{'}gso;
$toencode =~ s{\x8b}{‹}gso;
$toencode =~ s{\x9b}{›}gso;
if (defined $newlinestoo && $newlinestoo) {
$toencode =~ s{\012}{ }gso;
$toencode =~ s{\015}{ }gso;
}
}
return $toencode;
}
さらに問題を追跡すると、ブラウザのデフォルトが iso-8859-1 であることがわかり、手動で utf-8 に設定しても、文字列が iso-8859-1 としてサーバーに返されます。
ついに、
print header(-charset => qw(utf-8)), '<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
-charset => qw(utf-8) パラメーターをヘッダーに追加します。漢詩はやはり漢詩です。
Schwern のコメントに感謝します。問題を突き止めてリーソンを学ぶきっかけになりました。