in reply to Re: persistent new line
in thread persistent new line

chomp only bites off the last character of the string. Windows is using \r\n for newlines. So, from porting point of view, using s/\r?\n$//g is much more portable.

Another solution would be using chomp in combination with an conditional chop

Replies are listed 'Best First'.
Re^3: persistent new line
by Marshall (Canon) on Feb 16, 2012 at 23:23 UTC
    chomp only bites off the last character of the string

    That is not strictly true. Clearly on Windows, a chomp will remove both the CR and the LF - that's 2 characters! On Windows the IO layer is expecting CRLF terminated lines and it just automatically assumes that when opening a file in text mode. On Unix, trizen's suggestion of: open FILE, '<:crlf', 'index.html' looks like it would also work. Having said that, I don't see anything wrong with using a regex to remove whitespace from the end of the line - this approach will work fine.

    Maybe this is too "nit-picky", but this "one character" idea is not correct. In Perl "\n" is magical in that sometimes it also means CRLF instead of just the single character LF. Also consider that CRLF line endings are also used all the time on Unix!! This is the standard for network communications. So if you open a socket and write to it on any OS, these lines will also be CRLF terminated! Most of the time this stuff is pretty much transparent to the Perl programmer. This thread shows a case where it is not.