Is this a binary file or not???

This is a simple perl script that checks if the passed in input data is binary or not by checking if, within the first 8k the number of non-ascii characters exceeds the number of non-ascii characters by a factor of 2. It’s fundamental in that it doesn’t check for unicode files, so is probably broken in that case.

#!/usr/bin/perl -w
use strict;
my $buffer;
my $nread;
$nread = sysread(STDIN, $buffer, 8192);
my $nchars = 0;
my $nbinchars = 0;
foreach (split(/ */, $buffer)) {
my $value = ord;
if ($value < 32 || $value >= 127) {
$nbinchars++;
} else {
$nchars++;
}
}
exit ($nbinchars * 2 > $nchars);