Hi,

I want to get rid of the line ending troubles. Until now I had files which had these line endings: 0D 0A (Windows), 0A (Unix), 0D (early MAC). The last file I've seen had mixed MAC and Unix line endings and that was then the reason why my code was not working.

I am now seeking for a solution which is reading a file line per line independent of the three line ending types. Also my goal is it that the line ending is chomped, so that I only have the data of each line stored in an array.

I tried to solve this with the following code:

#!/usr/bin/perl use strict; use warnings; use Perl6::Slurp; # generate test file "le.txt" my $win_line = "Windows\r\n"; my $unix_line = "Unix\n"; my $mac_line = "Mac\r"; open(my $fh, ">", "le.txt"); binmode($fh); print $fh $win_line; print $fh $unix_line; print $fh $mac_line; close($fh); # read file with slurp my @lines = slurp("le.txt", {chomp => 1, irs => qr/(\r\n)|(\n)|(\r)/}) +; for my $line (@lines) { print $line . "\n"; }
But the read code is not working and I don't know why. Any hints are welcome. Here I wrote some other code to read the file with the differrent line endings. This code is working.
# read file my $file_content; { local $/ = undef; # no input data record separator --> file conten +t will be stored in one scalar open(my $fh, "<", "le.txt"); binmode($fh); $file_content = <$fh>; close($fh); } (my $space_file_content = $file_content) =~ s/(\r\n)|(\n)|(\r)/ /g; for my $line (split(/ /, $space_file_content)) { print $line . "\n"; }

Although the second solution is working it would be very interesting for me how you would solve this problem and why my slurp code is not working.

Thank you very much for your help.

Dirk


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