perlquestion bobf <p> I wrote a program that is being run on different platforms (Linux, Windows, and Mac). I tried to write it to be as system-independent as I could (using [doc://File::Spec] for paths, etc), but recently someone reported a bug. It turns out that she was creating one of the input files on a Mac, then transferring it to a Windows machine and running the program (I didn't think of that...). The error occurred when the program tried to read the input file line-by-line. I presume that since the program was run on a Windows machine, the input record separator ($/) was set to the Windows newline (\015\012). The input file was created on a Mac, though, so it had newlines of \015. As a result, the file got slurped and things turned ugly. </p> <p> Now I'm trying to figure out how to handle this situation. I reread [doc://perlport], as well as [id://420989] and [id://264423], but they all seem to address <i>writing</i> specific newline characters to files, not reading them. </p> <p> Here is what I came up with so far: <ol> <li> <strong>Use $^O</strong>, 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. </li> <li> <strong>Use a regex</strong> to match the newline character(s) in the file. I think this would require slurping the whole file and then doing something like <code>if( $file =~ m/\015$/ )</code> (which assumes the file will end with a newline) or <code>if( $file =~ m/\015(?!\012)/ )</code> (which doesn't), setting $/ according to what matched, and re-reading the file line-by-line. </li> <li> <strong>Preprocess the input file</strong> to convert all newline characters to the current system's newline character. I experimented a little, and I think this will work:<br> <code> $file =~ s[(\015)?\012(?!\015)][\n]g; $file =~ s[(\012)?\015(?!\012)][\n]g; </code><br> I think this is my favorite solution, but it seems like a lot of extra overhead for each input file since the conversion only needs to occur once (assuming the input file is not then moved to another OS). </li> </ol> </p> <p> Are there better ways of handling this? </p> <p> Thanks! </p>