我正在玩 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 来编辑文件并使其正常渲染。打印到文件期间存在问题。
一些基本信息:Strawberry perl 5.12 without use utf8;尝试使用 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 的评论,它启发了我去追查问题并学习 leeson。