Dealing with UTF8 in Perl

Read Later

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.


Feedback and support

For comments and questions, tweet out to @Rubenerd_Blog, or email me at weblog at ruben schade dot com. Let me know if you don't want your comment posted. I also accept the following donations or referrals if you found something useful or fun. Thank you :).