詩と創作・思索のひろば

ドキドキギュンギュンダイアリーです!!!

Fork me on GitHub

To avoid "Wide character in print" warnings in Test::More

Today I happend to read perldoc of Test::More and learned:

The Solution

Put this line before use Test::More.

use open ':std', ':encoding(utf8)';

The Problem

use utf8;
use strict;
use warnings;
use Test::More 'no_plan';

pass 'L( ◔ω◔)┘三└( ◔ω◔)」';

subtest 'foo' => sub {
    pass 'L( ◔ω◔)┘三└( ◔ω◔)」';
};
Wide character in print at /Users/motemen/.plenv/versions/5.14.2/lib/perl5/5.14.2/Test/Builder.pm line 1759.
ok 1 - L( ◔ω◔)┘三└( ◔ω◔)」
Wide character in print at /Users/motemen/.plenv/versions/5.14.2/lib/perl5/5.14.2/Test/Builder.pm line 1759.
    ok 1 - L( ◔ω◔)┘三└( ◔ω◔)」
    1..1
ok 2 - foo
1..2

The Explanation

use open ':std', ':encoding(utf8)' adds encoding(utf8) Perl IO layer to both STDOUT and STDERR (and STDIN), which Test::Builder duplicates to its output handles.

The following code will do the same:

BEGIN {
  binmode STDOUT, ':encoding(utf8)';
  binmode STDERR, ':encoding(utf8)';
}
use Test::More;

Reference

はてなで一緒に働きませんか?