in reply to detect incorrect character encoding

The first reply should work fine, unless your script generates other warnings unrelated to character encoding, and if you are simply testing for utf8 vs. some different (single-byte) encoding, then Encode::Guess should do nicely as well. Here's another way, which involves specifically testing that a given file is encoded correctly as utf8 data (with no errors, corruptions, or use of non-utf8 characters):
use Encode; my $filename = "whatever"; eval { open my $file, "<:raw", $filename or die $!; local $/; local $_ = <$file>; decode( "utf8", $_, Encode::FB_CROAK ); } die "$filename is invalid utf8: $@\n" if $@;
For more info on that, check the Encode man page, esp. the section titled "Handling Malformed Data".