One of my users had problems with an input text file. Windows printed the file ok from the command line. However, evidently the file had <CR> line endings (old Mac convention). With following code, I just got a few characters + some garbage from near the end of a few hundred line file:
open my $in, '<', "file" or die "$!"; #open worked while (my $line = <$in>){print $line;} #just a few characters print
I am running on Win 10, Active State Perl (v5.24.3). Above code will work with <LF> (Unix) or <CR><LF> (Windows) line endings but evidently not with <CR> line endings. My user has changed his workflow to only make <CR><LF> files and so things are fine moving forward. However, I am curious. I remember previous versions of Perl working fine when reading <CR> terminated lines. Any ideas?

Update fixed file handle typing mistake. More of actual code shown later in thread.

Update: Thanks for the interesting input, especially from LanX and haukex. Fortunately I don't have to solve the general problem at the moment (read text file with unknown line endings). When I wrote the code, I implemented it in such a way that Unix or Windows endings work fine for input and all files that I generate have pure line endings of the platform the code was run on. This <CR> file was a "weird duck". I spent some more time talking with my user and found out that he generated the <CR> file on a Windows platform. But had his text editor set to use <CR> line endings instead of Windows standard <CR><LF>! We agreed that this is "outside of spec" and "don't do that". Since the user symptom was "program didn't do anything" (no valid input records read), I've added a simple print of number of records processed. Program will exit with non-zero error code if that number is zero just in case this utility is ever used in a batch file. So things are fine now and I incrementally learned a bit.


In reply to 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.