加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 模板工具包字符编码

发布时间:2020-12-15 21:43:19 所属栏目:大数据 来源:网络整理
导读:似乎模板工具包没有正确处理编码. 我正在传递模板 – 处理文件名(在哪里获取模板),哈希引用(包含所有参数)和标量引用(在哪里放置输出)然后我返回它然后显示它到用户. 当我给它一个带有变音符号的字符串时,html输出包括一个黑色菱形,带有白色问号代替每个字母
似乎模板工具包没有正确处理编码.

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

当我给它一个带有变音符号的字符串时,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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读