0

I'm using Perl6::Form to generate a table and output it to a text file. No matter what I do, it seems, I can't output Windows line endings. I've tried local $OUTPUT_RECORD_SEPARATOR = "\r\n"; I've tried appending \r\n to my format strings. No dice.

My code:

use English;

local $OUTPUT_RECORD_SEPARATOR = qq{\r\n};

my @column_headings = @{ shift $args->{'data'} };
my @rows            = @{ $args->{'data'} };

my $header_format = join q{|}, (q/{]]]][[[[}/) x scalar @column_headings;
my $field_format  = join q{|}, (q/{]]]]]]]]}/) x scalar @column_headings;

# formatting starts with headers followed by double line
my @format_data = ( $header_format, @column_headings, );
push @format_data, join q{|}, (q/==========/) x scalar @column_headings;
foreach my $row (@rows) {
    push @format_data, ( $field_format, @{$row} );
}
my $text = form @format_data;

my ( $fh, $tempfile ) = File::Temp::tempfile;
$fh->print($text) or croak(qq/Failed to write to tempfile: $OS_ERROR/);
close $fh;
Aurelia Peters
  • 2,169
  • 1
  • 20
  • 34
  • Do you [`use English`](http://search.cpan.org/~dom/perl-5.12.5/pod/perlvar.pod#Predefined_Names)? Otherwise you can only access the output separator through `$\\`. – mob Feb 08 '13 at 22:04
  • Also, the variable name is `$OUTPUT_RECORD_SEPARATOR`. – mob Feb 08 '13 at 22:08
  • @mob Yes, I'm using `English` and `$OUTPUT_RECORD_SEPARATOR`. Will correct post. – Aurelia Peters Feb 08 '13 at 22:44

1 Answers1

6

According to the docs,

The file returned by File::Temp will have been opened in binary mode if such a mode is available. If that is not correct, use the C function to change the mode of the filehandle.

As such, re-add the :crlf normally present on file handles opened in Windows using the following after the open but before the print.

$fh->binmode(':crlf');
ikegami
  • 367,544
  • 15
  • 269
  • 518
cjm
  • 61,471
  • 9
  • 126
  • 175