2010-03-18 17 views
11

我有这个警告,每次我跑我的CGI脚本(输出由模板呈现::工具包):如何消除TT的“宽字符打印”警告?

Wide character in print at /usr/local/lib/perl5/site_perl/5.8.9/mach/Template.pm line 163.

什么是消除它的正确方法?

my %config = (
     ENCODING  => 'utf8', 
     INCLUDE_PATH => $ENV{TEMPLATES_DIR}, 
     EVAL_PERL => 1, 
} 
my $tt = Template->new(\%config); 

回答

8

调用$tt->process()之前将这个具有自动输出编码:

binmode STDOUT, ':utf8'; 

编辑:作为daxim提到的,这是

我使用这种配置创造了TT对象可能利用TT的编码设施:

$tt->process($infile, $vars, '-', { binmode => ':utf8' }) 

这取决于'-'文件名为STDIN打开阅读时的广泛使用的约定,而打开时会为其写入。

编辑2:顺便说一句,后一种方式似乎不适用于mod_perl(2.0.5)。

+1

http://ahinea.com/en/tech/perl-unicode-struggle.html – Quentin 2010-03-18 09:14:43

+0

这是否将输出自动编码为任何编码?如果是这样,那太棒了! – planetp 2010-03-18 09:24:36

+0

@planetp:是的。请参阅http://perldoc.perl.org/perlunifaq.html#Is-there-a-way-to-automatically-decode-or-encode? – 2010-03-18 09:32:15

1
$tt->process($infile, $vars, $outfile, { binmode => ':encoding(UTF-8)' }) 

这记录在http://search.cpan.org/perldoc?Template#process%28%24template%2C_%5C%25vars%2C_%24output%2C_%25options%29中。

+1

这似乎只在输出到文件时才起作用:'当前支持的唯一选项是binmode,当设置为任何真值时将确保创建的文件(但不是任何现有的文件句柄通过)将被设置为二进制模式。“ – 2010-03-18 12:19:59

+1

你的猜测是错误的。谨慎撤销你的投票?你为什么不先自己测试一下,而是自己试试呢? 'Template-> new-> process('foo [%bar%] quux',{bar =>“\ x {4e71}”},' - ',{binmode =>':encoding(GB2312)'}) ;' – daxim 2010-03-18 13:28:39

+0

我希望你的回答更好,如果它包括这个例子,并解释什么特殊的'“ - ”'文件名的意思是按惯例。 – 2010-03-18 22:04:32