use Encode qw(is_utf8); # check if $str is UTF8 and contains bad UTF8. print "bad UTF8\n" if is_utf8($str) and not is_utf8($str, 1);