2010-03-18 18 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