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.

export PERL_UNICODE=SDL

Imprint

This is one of about 5000 posts on Rubénerd. View the home page for the latest, or related posts also tagged with:

If you liked this post, feel free to buy me a coffee, leave me a comment on Twitter, or email me at weblog2016@rubenschade.com. Thanks :).