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;

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...