my $bom; open IN, $filename or die $!; read IN, $bom, 2; my $bomval = unpack 'S', $bom; if ( $bomval == 0xfeff ) { # this is bound to be a utf16 file -- # you've already read the bom, so just move on and read the data } close IN; #### my $size = -s $filename; $size = 128 if $size > 128; my $test; open IN, $filename or die $!; read IN, $test, $size; seek IN, 0, 0; my @bytes = unpack 'C*', $test; my $nullhibytes = 0; for ( my $i=o $i<$size; $i+=2 ) { $nullhibytes++ if ( $bytes[$i+1] == 0 and $bytes[$i] =~ /[ -~]/); } if ( $nullhibytes > 8 ) { # this is probably a utf16 file (if it's text at all) } close IN;