Dealing with UTF8 in Perl


If you have Perl warnings enabled (like a good developer), chances are you’ve seen something like this when executing your script:

Wide character in print at ./[file].pl line [x].

As with everything in Perl, there are Many Ways™ to deal with this.

The first is to include the following line along with your regular Perl boilerplate. I always write my scripts and text files in UTF8, so this merely enforces UTF8 output.

binmode(STDOUT, ":utf8");

This will deal with those warnings, and let you get on with your work.

An alternative is to enforce UTF8 for STDIN and STDERR, as well as STDOUT with this line.

use open qw/:std :utf8/;

And of course, there’s always a trusty environment variable.


Author bio and support


Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person in bios. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.