sub guess_newline { my $file = shift; open my $F, '<', $file or return undef; my ($buf, $sep); until ( eof($F) or defined $sep) { # read a 1-k chunk + a random few bytes read( $F, $buf, 1024+int(rand(5)) ); # the trailing dot below is important, in case part of a newline is # truncated in the read! $sep = $1 if $buf=~m/(\x0A|\x0D|\x0D\x0A)./; } close $F; return $sep; } #### { local $/ = guess_newline($filename) || die "Can't guess sep for $filename"; open my $IN, '<', $filename or die "Can't read $filename: $!"; while (<$IN>) { ... } }