Hi My script is reading through a text file line by line and may come across characters that are not UTF8 encoded properly and will get this message:

UTF-8 "\x97" does not map to Unicode at Level1QC.pl line 225, <$fh> line 25 (#1) (S utf8) When reading in different encodings, Perl tries to map everything into Unicode characters. The bytes you read in are not legal in this encoding.

My question is it possible to do something if it runs into this issue. I would like to move this file to a specific location if it contains bad UTF-8 characters. Here's the loop I'm using to read the file. The error is on the line while (my $line = <$fh>) Right now the script just ignores this line and moves on to the next line. I could even read the file beforehand to check for invalid characters if easier.

Hmm, there may be (or there ought to be) a better way, but you could possibly make the waning fatal (with the Fatal module) and then catch the exception (e.g. with Try::Tiny), and take it from there to more your file around.

I like Laurent's idea, but I would prefer to locally modify the SIG hash to use a custom warnings handler. This way I can build a hash of bad files perhaps listing the bad line numbers, without effecting other warnings. These can be handled later, or a short circuiting system could be implemented via the likes of Try::Tiny. Along the lines of:

In case its useful to you, here is an example more along the lines of Laurent's suggestion of trapping fatal warnings. It simply slurps the entire file ( easily modified to buffer if too large ), then pushes good and bad filepaths into individual arrays: