作者: WATANABE Hirofumi
日時: 2006/1/22(19:47)
わたなべです。

藤岡和夫 <kazuf@...> writes:

> #!/cygdrive/c/Perl5.8/bin/perl
> ##!/usr/bin/perl
> use utf8;
> my $str = "日本語";# UTF-8で書く
> print "$str: ",length $str,"\n";
> foreach (split //, $str){
> 	print sprintf("%04X\x20", ord);
> }
> 
> のようなサンプルを動かすと、
> 
> $ ./utf8test.pl
> Wide character in print at ./utf8test.pl line 5.
> 日本語: 3
> 65E5 672C 8A9E 
> 
> のように出力されるのですが、Wide character in printのメッセージはなぜ出
> るのでしょうか。これはエラー?

perldoc perldiagによると

Wide character in %s
    (W utf8) Perl met a wide character (>255) when it wasn’t expecting
    one.  This warning is by default on for I/O (like print).  The eas-
    iest way to quiet this warning is simply to add the ":utf8" layer
    to the output, e.g. "binmode STDOUT, ’:utf8’".  Another way to turn
    off the warning is to add "no warnings ’utf8’;" but that is often
    closer to cheating.  In general, you are supposed to explicitly
    mark the filehandle with an encoding, see open and "binmode" in
    perlfunc.

なのでbinmodeする必要があります。

-- 
わたなべひろふみ