【问题标题】:I get extra CR using TT (perl template toolkit)我使用 TT(perl 模板工具包)获得了额外的 CR
【发布时间】:2012-02-03 03:37:44
【问题描述】:

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

源文本(windows格式):

"Some_html" CR LF  

输出文本:

"Some_html" CR  
CR LF

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

"Some_html" LF   

输出文本:

"Some_html" CR LF

(我使用 notepad++ 显示 CR 和 LF 字符;也用于更改源模板中的 unix windows 格式)。

当我在谷歌上搜索问题时,我在 Windows 上收到了一些(很少)关于额外 ^M 的帖子,但我找不到根本原因的解释,也不是真正的解决方案(只是一些解决方法如何摆脱额外的^M)。

虽然不是真正的问题,但我觉得它很“不干净”。
是否有一些我应该打开的配置(我查看了www.template-toolkit.org/docs/manual/Config.html 但找不到任何东西)?
其他一些解决方案? (除了对输出文件进行后期修复)。
谢谢

【问题讨论】:

    标签: perl template-toolkit


    【解决方案1】:

    Template Toolkit 以二进制模式读取模板的源文件,但以文本模式写入。来自模板的数据(包含 CR LF)在以文本模式输出时被翻译,因此 LF 变为 CR LF。

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

    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();
    

    【讨论】:

      【解决方案2】:

      不幸的是,bvr 的解决方案不适用于使用[% FILTER redirect(...) %] 生成的输出。在 Windows 10 上,模板

      [% FILTER redirect("bar.txt") %]
      This text is for bar.txt.
      [% END %]
      This text is for foo.txt.
      

      (以 DOS 样式的 CR-LF 行结尾)扩展为

      #! /bin/perl
      use strict;
      use warnings;
      
      use Template;
      
      my $tt = Template->new({
                              OUTPUT_PATH => '.',
                              RELATIVE => 1,
                             }) || die "$Template::ERROR\n";
      
      my $srcfile = 'foo.txt.tt';
      my $tgtfile = 'foo.txt';
      
      open my $ofh, '>:raw', $tgtfile or die;
      
      $tt->process($srcfile, {}, $ofh, { binmode => ':raw' })
          || die $tt->error . "\n";
      

      使用预期的 CR-LF 行结尾创建输出文件 foo.txt,但创建带有错误 CR-CR-LF 行结尾的 bar.txt:

      > od -c bar.txt
      0000000  \r  \r  \n   T   h   i   s       t   e   x   t       i   s
      0000020   f   o   r       b   a   r   .   t   x   t   .  \r  \r  \n
      0000037
      

      我在https://github.com/abw/Template2/issues/63向TT作者报告了这个问题。

      我找到了一个简单的解决方法:在子 Template::_output(在 Template.pm 中)中,更改

      my $bm = $options->{ binmode };
      

      my $bm = $options->{ binmode } // $BINMODE;
      

      然后在你的主要 perl 脚本集中

      $Template::BINMODE = ':raw';
      

      然后您可以使用处理模板

      $tt->process($srcfile, {}, $tgtfile) || die $tt->error . "\n";
      

      并在主输出和重定向输出中获取 CR-LF 行结尾。

      【讨论】:

        【解决方案3】:

        祝你有美好的一天, 我找到了一个非常简单的解决方案:

        my $tt = Template->new({
                ...,
                PRE_CHOMP  => 1,
                POST_CHOMP => 1,
                ...
        });
        

        此配置将指示模板引擎删除模板文本​​的所有前后 CR LF。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-21
          • 2011-12-26
          • 2019-05-29
          • 2012-12-23
          • 2014-01-08
          • 1970-01-01
          • 1970-01-01
          • 2013-12-27
          相关资源
          最近更新 更多