in reply to persistent new line

First of all thank you all for response, Perl wisdom shall be with you :P

Solution was to add:   $line =~ s/\r//g;

In response to answers:

1. Isn't  chomp $line; chomping this line?

2. I know that the proper way is to use strict and warnings - thanks for remaining me I need to get use to use it (sounds funny).

3. I tend to keep things simple in meaning of understanding from "just look" approach this is why I am not using any library's like LWP::UserAgent etc. if its not making me writing faster and can be replaced with more simple code in my opinion.

Replies are listed 'Best First'.
Re^2: persistent new line
by Anonymous Monk on Feb 16, 2012 at 06:09 UTC

    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

      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.