in reply to Unix and Windows CRLF vs LF
I asked a similar question in Newlines: reading files that were created on other platforms. In summary, I started with these options:
- Use $^O, but if I understand it correctly that will just tell me about the system the program is running on, which (as exemplified here) is not necessarily the same as the system that created the file.
- Use a regex to match the newline character(s) in the file. I think this would require slurping the whole file and then doing something like if( $file =~ m/\015$/ ) (which assumes the file will end with a newline) or if( $file =~ m/\015(?!\012)/ ) (which doesn't), setting $/ according to what matched, and re-reading the file line-by-line.
- Preprocess the input file to convert all newline characters to the current system's newline character. I experimented a little, and I think this will work:
$file =~ s[(\015)?\012(?!\015)][\n]g; $file =~ s[(\012)?\015(?!\012)][\n]g;
I ended up implementing the preprocessing solution, but I would probably use binmode if I were to do it today.