문제

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;
도움이 되었습니까?

해결책

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');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top