title: Properly detect line-endings of a file in Perl?
body: <p>Problem: I have data (mostly in CSV format) produced on both Windows and *nix, and processed mostly on *nix.   Windows uses CRLF for line endings and Unix uses LF.  For any particular file I don't know whether it has windows or *nix line endings.  Up until now, I've been writing something like this to handle the difference:</p>
  
  <pre><code>while (&lt;$fh&gt;){
      tr/\r\n//d;
      my @fields = split /,/, $_;
      # ...
  }
  </code></pre>
  
  <p>On *nix the \n part is equivalent to chomping, and additionally gets rid of \r (CR) if it's a windows-produced file.  </p>
  
  <p>But now I want to Text::CSV_XS b/c I'm starting to get weirder data files with quoted data, potentially with embedded line-breaks, etc.  In order to get this module to read such files, Text::CSV_XS::getline() requires that you specify the end-of-line characters.  (I can't read each line as above, tr/\n\r//d, and them parse it with Text::CSV b/c that wouldn't handle embedded line-breaks properly).  How do I <em>properly</em> detect whether an arbitrary file uses windows or *nix style line endings, so I can tell Text::CSV_XS::eol() how to chomp()?</p>
  
  <p>I couldn't find a module on CPAN that simply detects line endings. I don't want to to first convert all my datafiles via dos2unix, b/c the files are huge (hundreds of gigabytes), and spending 10+ minutes for each file to deal with something so simple seems silly. I thought about writing a function which reads the first several hundred bytes of a file and counts LF's  vs CRLF's, but I refuse to believe this doesn't have a better solution. </p>
  
  <p>Any help?</p>
  
  <p>Note: all files are either have entirely windows-line endings or *nix endings, ie, they are not both mixed in a single file.</p>