I don't think print is the appropriate debugging tool here...
$ hexdump -C foo.txt 00000000 46 6f 6f 0d 42 61 72 0d 51 75 7a 0d |Foo.Bar.Q +uz.| 0000000c $ cat read.pl #!/usr/bin/env perl use warnings; use strict; use Data::Dumper; $Data::Dumper::Useqq=1; open my $fh, '<', 'foo.txt' or die $!; print Dumper([PerlIO::get_layers($fh)]) unless $] lt '5.008'; while (<$fh>) { print Dumper($_); } close $fh;
Output on Win 7 Strawberry Perl 5.10, 5.14, 5.20, and 5.26:
$VAR1 = [ "unix", "crlf" ]; $VAR1 = "Foo\rBar\rQuz\r";
And on Linux, the output is as follows:
# 5.6.2: $VAR1 = "Foo\rBar\rQuz\r"; # 5.8.1 and 5.8.9: $VAR1 = [ "stdio" ]; $VAR1 = "Foo\rBar\rQuz\r"; # 5.10.1 thru 5.26: $VAR1 = [ "unix", "perlio" ]; $VAR1 = "Foo\rBar\rQuz\r";
So none of the tested versions handle a CR line ending. See also open: the default layers are :raw on Unix and :crlf on Windows. For details see also PerlIO and open (the pragma). (Update: and LanX provided another excellent link with Newlines in perlport)
If the file is plain ASCII, this works across all of the above configurations:
use warnings; use strict; open my $fh, '<', 'foo.txt' or die $!; binmode $fh; $/="\x0D"; while (<$fh>) { chomp; ... } close $fh;
In reply to Re: Reading text file with <CR> line endings
by haukex
in thread Reading text file with <CR> line endings
by Marshall
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |