我使用TT(Perl模板工具包)获得了额外的CR [英] I get extra CR using TT (perl template toolkit)

查看:132
本文介绍了我使用TT(Perl模板工具包)获得了额外的CR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用perl v5.10(在Windows 7上)+ TT v2.22.当我使用TT时,对于每个源代码行,我都会在生成的html中获得一个额外的CR:

I use perl v5.10 (on windows 7) + TT v2.22. When I use TT, for each source line, I get in the produced html an extra CR :

源文本(Windows格式):

Source text (windows format):

"Some_html" CR LF  

输出文本:

"Some_html" CR  
CR LF

但是,当我将源文件转换为unix格式,然后运行TT时,我得到了:
源文本(unix格式):

However, when I convert my source file to unix format, and then I run TT, I get :
Source text (unix format):

"Some_html" LF   

输出文本:

"Some_html" CR LF

(我使用notepad ++显示CR& LF字符;也更改了源模板中的UNIX<-> Windows格式).

(I use notepad++ to show the CR & LF characters; also to change unix <-> windows formats in the source template).

当我用Google搜索问题时,我在Windows上收到一些(很少)关于多余的^M的帖子,但是我找不到关于根本原因的解释,也没有真正的解决方案(只是一些解决方法,如何摆脱了多余的麻烦^M).

When I google the problem, I get some (few) posts about extra ^M on windows, but I couldn't find explanation as for the root cause neither a true solution (just some workaround how to get rid of extra ^M).

虽然不是一个真正的问题,但我发现它很脏".
是否有一些我应该打开的配置(我查看了 www.template-toolkit.org/docs/manual/Config.html ,但找不到任何内容)?
其他解决方案? (除了后固定输出文件).
谢谢

Although not a real problem, I find it quite "unclean".
Is there some configuration that i should turn on (I reviewed www.template-toolkit.org/docs/manual/Config.html but could not find anything) ?
Some other solution ? (other than post-fixing the output file).
Thanks

推荐答案

Template Toolkit 以二进制模式读取模板的源文件,但是以文本模式编写.模板(包含CR LF)中的数据在文本模式下的输出过程中被转换,因此LF变为CR LF.

Template Toolkit reads source files for templates in binary mode, but writing in text mode. Data from template (that contains CR LF) are translated during output in text mode, so the LF becomes CR LF.

该问题的最简单解决方案是以二进制模式写入文件(注意对open调用的raw修饰符):

The easiest solution for the problem is to write files in binary mode (note the raw modifier to open call):

my $tt = Template->new;
my $output_file = 'some_file.txt';
open my $out_fh, '>:raw', $output_file    or die "$output_file: $!\n";
$tt->process('template', \%data, $out_fh) or die $tt->error();

这篇关于我使用TT(Perl模板工具包)获得了额外的CR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆