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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.