三生石@ 2013-03-04 20:29 采纳率: 100%
浏览 253
已采纳

使用'Use utf8;'给出了'Wide character in print'

If I run the following Perl program:

perl -e 'use utf8; print "鸡\n";'

I get this warning:

Wide character in print at -e line 1.

If I run this Perl program:

perl -e 'print "鸡\n";'

I do not get a warning.

I thought use utf8 was required to use UTF-8 characters in a Perl script. Why does this not work and how can I fix it? I'm using Perl 5.16.2. I have the same issue if this is in a file instead of being a one liner on the command line.

转载于:https://stackoverflow.com/questions/15210532/use-of-use-utf8-gives-me-wide-character-in-print

  • 写回答

6条回答 默认 最新

  • Memor.の 2013-03-05 10:56
    关注

    Without use utf8 Perl interprets your string as a sequence of single byte characters. There are four bytes in your string as you can see from this:

    $ perl -E 'say join ":", map { ord } split //, "鸡\n";'
    233:184:161:10
    

    The first three bytes make up your character, the last one is the line-feed.

    The call to print sends these four characters to STDOUT. Your console then works out how to display these characters. If your console is set to use UTF8, then it will interpret those three bytes as your single character and that is what is displayed.

    If we add in the utf8 module, things are different. In this case, Perl interprets your string as just two characters.

    $ perl -Mutf8 -E 'say join ":", map { ord } split //, "鸡\n";'
    40481:10
    

    By default, Perl's IO layer assumes that it is working with single-byte characters. So when you try to print a multi-byte character, Perl thinks that something is wrong and gives you a warning. As ever, you can get more explanation for this error by including use diagnostics. It will say this:

    (S 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 easiest 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 perlfunc/binmode.

    As others have pointed out you need to tell Perl to accept multi-byte output. There are many ways to do this (see the Perl Unicode Tutorial for some examples). One of the simplest ways is to use the -CS command line flag - which tells the three standard filehandles (STDIN, STDOUT and STDERR) to deal with UTF8.

    $ perl -Mutf8 -e 'print "鸡\n";'
    Wide character in print at -e line 1.
    鸡
    

    vs

    $ perl -Mutf8 -CS -e 'print "鸡\n";'
    鸡
    

    Unicode is a big and complex area. As you've seen, many simple programs appear to do the right thing, but for the wrong reasons. When you start to fix part of the program, things will often get worse until you've fixed all of the program.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 问题重发,R语言:代码运行过程中出现如下警告,请求如何解决!
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 苍穹外卖拦截器token为null
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关