in reply to Re^2: Print Behavior with Carriage Return
in thread Print Behavior with Carriage Return
However, this is only a good idea if your open statement opens only windows files.
Why do you say this? The :crlf layer doesn't hurt when opening files that have LF only. The following test passes on both Linux and Windows.
use warnings; use strict; use File::Temp qw/tempfile/; my %tf = ( crlf => do { my ($tfh, $tfn) = tempfile(UNLINK=>1); binmode $tfh; print $tfh "Foo\r\nBar\r\nQuz\r\n"; close $tfh; $tfn }, lf => do { my ($tfh, $tfn) = tempfile(UNLINK=>1); binmode $tfh; print $tfh "Foo\nBar\nQuz\n"; close $tfh; $tfn }, mix => do { my ($tfh, $tfn) = tempfile(UNLINK=>1); binmode $tfh; print $tfh "Foo\r\nBar\nQuz\r\n"; close $tfh; $tfn }, ); sub slurp { my ($fn, $mode) = @_; open my $fh, '<'.$mode, $fn or die "$fn: $!"; local $/ = undef; return scalar <$fh>; } use Test::More tests=>10; is $/, "\n"; is slurp($tf{crlf}, ':raw' ), "Foo\r\nBar\r\nQuz\r\n"; is slurp($tf{lf}, ':raw' ), "Foo\nBar\nQuz\n"; is slurp($tf{mix}, ':raw' ), "Foo\r\nBar\nQuz\r\n"; is slurp($tf{crlf}, ':crlf'), "Foo\nBar\nQuz\n"; is slurp($tf{lf}, ':crlf'), "Foo\nBar\nQuz\n"; is slurp($tf{mix}, ':crlf'), "Foo\nBar\nQuz\n"; is slurp($tf{crlf}, '' ), $^O eq 'MSWin32' ? "Foo\nBar\nQuz\n" : "Foo\r\nBar\r\nQuz\r\n"; is slurp($tf{lf}, '' ), $^O eq 'MSWin32' ? "Foo\nBar\nQuz\n" : "Foo\nBar\nQuz\n"; is slurp($tf{mix}, '' ), $^O eq 'MSWin32' ? "Foo\nBar\nQuz\n" : "Foo\r\nBar\nQuz\r\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Print Behavior with Carriage Return
by BillKSmith (Monsignor) on Jan 06, 2020 at 04:55 UTC | |
by haukex (Archbishop) on Jan 06, 2020 at 10:53 UTC |