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
    Yes, :crlf processes unix style files correctly, but for the wrong reason. (It does not translate Unix record separators, it fails to recognize them at all. This works because the Unix record separator and the perl newline are the same character.) I find this misleading, and recommend against it.
    Bill
      Yes, :crlf processes unix style files correctly, but for the wrong reason. (It does not translate Unix record separators, it fails to recognize them at all.

      I still don't quite follow - AFAIK, on *NIX, Perl's "logical" \n is always LF, so I don't really see any problems if it's just CRLF vs. LF files and only Windows and *NIX are involved; if there are other OSes or formats involved, then one might indeed run into issues there. But of course I'd also advise to always know one's input format (that gets especially important when Unicode is involved), so knowing whether one's input is CRLF vs. LF is a good recommendation of course. (In this particular case I think the :crlf layer gives more flexibility over input formats.)