I see that you have resolved your problem while I was composing this response but I might as well post it anyway in case it is helpful.

As you found, chomping MS Windows line terminators on a *nix type system without taking extra measures causes problems. Using split, ord, sprintf and map can be helpful in visualising what is happening to your data, it is much clearer than just printing each line. Your solution is to modify the default line terminator, which is the second item in the following code. Note the difference pre-chomp between that method and the :crlf I/O layer in my third item which actually removes the carriage return during the readline leaving just the line feed.

use strict; use warnings; use feature qw{ say }; my $winLineTerm = qq{\x0d\x0a}; my( $file, $writeFH, $readFH ); say q{-} x 20; open $writeFH, q{>}, \ $file or die $!; print $writeFH qq{ABC${winLineTerm}DEF$winLineTerm}; close $writeFH or die $!; say q{No measures}; open $readFH, q{<}, \ $file or die $!; while ( <$readFH> ) { say q{Before chomp()}; say for map { sprintf q{ %#02x}, ord } split m{}; say q{After chomp()}; chomp; say for map { sprintf q{ %#02x}, ord } split m{}; } close $readFH or die $!; say q{-} x 20; say q{Change default line terminator}; { local $/ = $winLineTerm; open $readFH, q{<}, \ $file or die $!; while ( <$readFH> ) { say q{Before chomp()}; say for map { sprintf q{ %#02x}, ord } split m{}; say q{After chomp()}; chomp; say for map { sprintf q{ %#02x}, ord } split m{}; } close $readFH or die $!; } say q{-} x 20; say q{open() with :crlf I/O layer}; open $readFH, q{<:crlf}, \ $file or die $!; while ( <$readFH> ) { say q{Before chomp()}; say for map { sprintf q{ %#02x}, ord } split m{}; say q{After chomp()}; chomp; say for map { sprintf q{ %#02x}, ord } split m{}; } close $readFH or die $!; say q{-} x 20;

The output.

-------------------- No measures Before chomp() 0x41 0x42 0x43 0xd 0xa After chomp() 0x41 0x42 0x43 0xd Before chomp() 0x44 0x45 0x46 0xd 0xa After chomp() 0x44 0x45 0x46 0xd -------------------- Change default line terminator Before chomp() 0x41 0x42 0x43 0xd 0xa After chomp() 0x41 0x42 0x43 Before chomp() 0x44 0x45 0x46 0xd 0xa After chomp() 0x44 0x45 0x46 -------------------- open() with :crlf I/O layer Before chomp() 0x41 0x42 0x43 0xa After chomp() 0x41 0x42 0x43 Before chomp() 0x44 0x45 0x46 0xa After chomp() 0x44 0x45 0x46 --------------------

I hope this is of interest.

Cheers,

JohnGG


In reply to Re: [SOLVED]: Problem when using chomp on line read from file. by johngg
in thread [SOLVED]: Problem when using chomp on line read from file. by Perl300

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.