in reply to handling different new line characters
Instead of chomp($line), you could use a regex:
$line =~ s/[\n\r]//g; [download]
This way you're handling both the CR and the LF.