The chomp function removes the current 'input record separator' (stored in $/, see perlvar) from the end of a given string of text. You have two options to make it behave in the given circumstance. I will assume, from here, that you are reading lines from a file with Windows-style line-endings (\r\n).

First, you could simply adjust your $/:

local $/ = "\r\n"; while (<DATA>) { chomp $_; print STDERR "'$_'\n"; }

Of course, if you don't know what kind of line endings you have, you can simply convert all line endings to newlines first:

while (<DATA>) { s/\r[\n]*/\n/gm; # now, an \r (Mac) or \r\n (Win) becomes \n (UNIX +) chomp $_; print STDERR "'$_'\n"; }

Seems a waste to run a regex and chomp, but you could remove chomp from the code above, and replace the regex with:

s/\r[\n]*//gm;

Of course, if your input separator isn't set, you'll read the whole file on the first pass through the while loop. You might consider an alternate strategy:

open FH, '<', $file or die "Can't read '$file': $!"; # find out what kind of line endings we have my $buffer; local $/ = undef; while ( read( FH, $buffer, 1024 ) ) { if ( $buffer=~m/(\r[\n]*)/s ) { $/ = $1; # set the input separator to what we found last; # stop trying to find the separator } } close FH; # now reopen the FH and read line by line open FH, '<', $file or die "Can't read '$file': $!"; while (<FH>) { chomp; print STDERR "'$_'\n"; } close FH;

There are cases I haven't dealt with, etc. for purposes of simplicity.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

In reply to Re: How to remove a carriage return (\r\n) by radiantmatrix
in thread How to remove a carriage return (\r\n) by monkfan

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.