微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl – 模板工具包字符编码

似乎模板工具包没有正确处理编码.

我正在传递模板 – >处理文件名(在哪里获取模板),哈希引用(包含所有参数)和标量引用(在哪里放置输出)然后我返回它然后显示它到用户.

当我给它一个带有变音符号的字符串时,html输出包括一个黑色菱形,带有白色问号代替每个字母(但字母数正确).任何其他角色都很好.

在我调用模板 – >进程之前,我正在使用警告打印出字符串,此时它很好,从模板 – >进程调用期间可以看出事情变成了垃圾.

有任何想法吗?
我尝试过使用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,$template->error();
return $data;

我感谢你们所有的时间:)

解决方法

以下代码有效. $data,特别是包含的字符串必须是Perl字符串,即正确的 decoded.见 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;

原文地址:https://www.jb51.cc/Perl/241642.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐