Yet another way would be to write a custom PerlIO layer (similar in spirit to :crlf, but for reading only), e.g. using PerlIO::via.

In its most simple form it could look something like:

package PerlIO::via::AnyCRLF; # save as PerlIO/via/AnyCRLF.pm sub PUSHED { my ($class) = @_; my $dummy; return bless \$dummy, $class; } sub FILL { my ($self, $fh) = @_; my $len = read $fh, my $buf, 4096; if (defined $buf) { $buf =~ s/\r\n/\n/g; $buf =~ s/\r/\n/g; } return $len > 0 ? $buf : undef; } 1;

Sample usage:

#!/usr/bin/perl use PerlIO::via::AnyCRLF; open my $f, "<:via(AnyCRLF)", "le.txt" or die $!; print while <$f>;

Handling the corner case (when \r\n gets split such that \r is in one buffer read, and \n in the next) is left as an exercise for the reader ;) — A quick fix could be to delegate the \r\n to \n translation to the regular :crlf layer (i.e. "<:crlf:via(AnyCRLF)"), and only do the \r to \n translation in this layer...


In reply to Re: line ending troubles by almut
in thread line ending troubles by Dirk80

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.