ActiveState Community

chomp leaves "0D"

Posted by bobinyec on 2009-06-03 15:45
OS: Windows

Lines in a file have "0D0A" at the end of each line. I'm trying to get rid of that. Using chomp gets rid of the "0A", but not the "0D". I've tried referring to this hex string as &#0D but I'm not having any success. Can anyone make a suggestion?

cristina_v | Fri, 2009-06-19 11:31

chomp() will remove whatever is in the variable $/, which is by default the newline character: x0A or \n or "line feed". For Windows systems new lines are delimited by x0D0A as you mentioned ("carriage return, line feed"). So to make sure chomp works as you would expect, redefine $/ as a local variable for your scripts that process Windows files.

local $/ = "\r\n";

http://www.perl.com/lpt/a/848 - "Field Record Separators"