Beware of wide characters

Before diving into examples we need to take precaution against a very common problem when dealing with Unicode in Perl. When you will start trying to output some non-ASCII characters, chances are you will run into following warning message:

Wide character in say in ./my_script.pl line 3

Well, what's that? What is wide character? How did it sneak into my coding chef d'œuvre?!

This warning usually happens when you output a Unicode string to a non-unicode filehandle, i.e. a filehandle with no unicode-compatible IO layer on it. IO layers is kinda close topic but we won't go into it right now, instead we'll show you possible solution to avoid this warning:

binmode FILEHANDLE, ":encoding(UTF-8)";

This command should be put before your printing statement and it will specify the encoding layer for desired FILEHANDLE. So, to be able to print to console FILEHANDLE should be STDOUT, which is the filehandle used by Perl's print and say functions by default.