in reply to How to remove a carriage return (\r\n)

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