Dealing with UTF8 in Perl

Software

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.

export PERL_UNICODE=SDL

Author bio and support

Me!

Ruben Schade is a technical writer and IaaS engineer in Sydney, Australia who refers to himself in the third person in bios. Wait, not BIOS… my brain should be EFI by now.

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

If you found this post helpful or entertaining, you can shout me a coffee or buy some silly merch. Thanks!