use constant HIGHEST_CHAR_ON_KBD => 126, #These values may differ for you, depending on where you bought LOWEST_CHAR_ON_KBD => 9; # your keyboard. There are some extra, non-keyboard chars in this range, as well. while( ) { foreach( split("", $_) ) { if( ( ord($_) > HIGHEST_CHAR_ON_KBD ) || ( ord($_) < LOWEST_CHAR_ON_KBD) ) { say "It's a binary file"; last; } } }