my $utf8; { local $^W; # warnings are thrown when unpacking # invalid UTF8, ignore them, we'll check # for problems be seeing if the string is # null at the end and shouldn't be. $utf8 = pack "U*", unpack "U*", $raw; } deal_with_malformed_utf8() if length $utf8 == 0 and length $raw != 0; #### use Encode; my $string = decode_utf8($bytes); deal_with_malformed_utf8() unless defined $string # or utf8::decode($string = $bytes) # of course, you can just or deal_with_malformed_utf8();# use one variable, and not # keep the raw bytes.