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

Delicious metadata


Liked this post?

Feel free to show you support below, and thank you.