Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

say i have template like
[% SET greet = 'HELLo' -%] [% greet | lower | ucfirst %]
with \r\n file endings I get double \r
$ tpage hello.tt |od -tacx1 0000000 H e l l o cr cr nl H e l l o \r \r \n 48 65 6c 6c 6f 0d 0d 0a 0000010
with \r file endings I get \r prefix
$ tpage hello.tt |od -tacx1 0000000 cr H e l l o cr \r H e l l o \r 0d 48 65 6c 6c 6f 0d 0000007
with \n file endings I get \r\n
$ tpage hello.tt |od -tacx1 0000000 H e l l o cr nl H e l l o \r \n 48 65 6c 6c 6f 0d 0a 0000007
I'm on win32, so i like to use \r\n, how to get tpage to like it?

Replies are listed 'Best First'.
Re: Template-Toolkit (tt2) tpage newline confusion
by Corion (Patriarch) on Jul 12, 2011 at 11:12 UTC

    It seems that tpage has a --binmode option that sets at least the binmode for its output. Grepping the dist for "binmode" yields some other entries in Changes, and somewhere in Template::Config, but I didn't find them well-documented. Template itself lists the { binmode => 1 } option for output in ->process, but doesn't seem to tell how to do the same for input (other than reading the file yourself, and pre-munging it).

    Update: I would call this a bug. The ttree program has the --binmode option, while the tpage program lacks it. Most likely you can just transplant the code from here to there.

      Most likely you can just transplant the code from here to there.

      I tried that but I couldn't get it to work. binmode STDOUT; did work for my platform.

      --- tpage 2011-07-12 04:27:14.140625000 -0700 +++ tpage-new2 2011-07-12 04:42:08.796875000 -0700 @@ -39,6 +39,7 @@ # unshift any perl5lib directories onto front of INC unshift(@INC, @{ $config->perl5lib }); +my $binmode = $config->binmode; # get all template_* options from the config and fold keys to UPPER C +ASE my %ttopts = $config->varlist('^template_', 1); my $ttmodule = delete($ttopts{ module }); @@ -67,10 +68,13 @@ my $template = $ttmodule->new($ucttopts) || die $ttmodule->error(); +binmode STDOUT; # process each input file foreach my $file (@ARGV) { $file = \*STDIN if $file eq '-'; - $template->process($file) +#~ $template->process($file, undef, \*STDOUT, $binmode ? {binmode + => $binmode} : {} ) +#~ $template->process($file, undef, \*STDOUT, {binmode => !!$binm +ode} ) + $template->process($file, undef, \*STDOUT, ) || die $template->error(); } @@ -83,6 +87,8 @@ ERROR => sub { die(@_, "\ntry `$NAME --help'\n") } }, 'help|h' => { ACTION => \&help }, + 'binmode=s', + 'template_encoding|encoding=s', 'template_absolute|absolute' => { DEFAULT => 1 }, 'template_relative|relative' => { DEFAULT => 1 }, 'template_module|module=s', @@ -167,6 +173,10 @@ --perl5lib=DIR Specify additional Perl library directori +es --template_module=MODULE Specify alternate Template module +File encoding options + --binmode=value Set binary mode of output files + --encoding=value Set encoding of input files + See 'perldoc tpage' for further information. END_OF_HELP

      I also found Template::Parser::LocalizeNewlines which also works

      --- tpage 2011-07-12 04:27:14.140625000 -0700 +++ tpage-new 2011-07-12 04:38:37.093750000 -0700 @@ -26,6 +26,7 @@ use strict; use Template; use AppConfig; +use Template::Parser::LocalizeNewlines; my $NAME = "tpage"; my $VERSION = 2.70; @@ -61,6 +62,8 @@ # add current directory to INCLUDE_PATH unshift(@{ $ucttopts->{ INCLUDE_PATH } }, '.'); +$ucttopts->{ PARSER } = Template::Parser::LocalizeNewlines->new ; + # read from STDIN if no files specified push(@ARGV, '-') unless @ARGV;