似乎模板工具包没有正确处理编码。
我路过template->process
一个文件名(获取模板的位置)、一个散列引用(包含所有参数)和一个标量引用(放置输出的位置)然后我将其返回并依次将其显示给用户。
当我给它一个带有变音符号的字符串时,html 输出包含一个黑色菱形,上面有一个白色问号代替每个字母(但字母数量正确)。任何其他角色都很好。
我在调用模板-> 进程之前使用警告打印出字符串,此时它很好,据我所知是在 template->process
期间。称事情变成了垃圾。
有任何想法吗?
我试过使用 ENCODING => "utf8"
以及 binmode => ":utf8"
但两者都对输出没有任何影响。
这是我的代码,删除了一些脂肪,只是为了显示我对模板-> 进程的调用,请注意,如果我省略了 {binmode => 'utf8'}
它没有效果。
<put variables in hash referenced to by vars>
<print out variables in has referenced to by $var>
my $data;
$template->process( $self->filename, $vars, \$data, {binmode => ':utf8'}) || die "Template process failed: ", $template->error();
return $data;
已解决
嘿,谢谢你的回答,问题是在模板进程完成它的事情后,我们然后在输出之前将字符串写入一个临时文件,所以我们还需要为文件设置 binmode,代码现在看起来像:
<put variables in hash referenced to by vars>
<print out variables in has referenced to by $var>
my $data;
binmode( STDOUT, ":utf8" );
$template->process( $self->filename, $vars, \$data, {binmode => ':utf8'}) || die "Template process failed: ", $template->error();
return $data;
我感谢大家的时间:)
最佳答案
下面的代码有效。 $data
,特别是包含的字符串必须是 Perl 字符串,即正确的 decode d.见 introduction to encoding in the official documentation .
use Template '2.21_02';
my $tt = Template->new({
ENCODING => 'utf8',
# other options …
});
$tt->process(
$template, $data, $output, {binmode => ':utf8'}
) or die $tt->error . ' in ' . $template;
关于perl - 模板工具包字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4486304/