http://www.perlmonks.org?node_id=214509


in reply to Line endedness

The requirements implied in your question are slightly unclear.
I need to process a text file but cannot be sure ahead of time if it's a DOS file (\r\n) or a Unix file (\n).
What platform is your script running on, and how is it getting access to the file?

If the file is a native file, "\n" will be the line terminator. If the file is being accessed over NFS or Samba, this software performs mapping of line terminators if the mount has been set up correctly.

However, there are instances when the file has been transferred by some other means, and you want to respect the termination. See perldoc perlvar on $INPUT_RECORD_SEPARATOR, $/, for how to change the default action of the <> operator, and what kind of a newline it is expecting.

Hope this helps,

--rW

Update: Just thought of a few other tips given what you are trying to do. You probably want to consider the output side as well, i.e. $OUTPUT_RECORD_SEPARATOR, $\. You might need a binmode on the file.

The last time I was doing something similar, I found perldoc perlport invaluable.