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

mobiGeek has asked for the wisdom of the Perl Monks concerning the following question:

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). I want to make changes to the file (add/del/modify lines), and want to preserve its line-endedness.

One way is to detect line-endedness

$first_line =~ m@((\r)?\n)@ and $NL = $1;
and then use $NL for any newlines, but I wonder if there isn't a niftier, Perlish way?