How do I read UTF-8 with diamond operator (<>)?

前端 未结 4 1527
暖寄归人
暖寄归人 2020-12-04 10:23

I want to read UTF-8 input in Perl, no matter if it comes from the standard input or from a file, using the diamond operator: while(<>){...}.

So m

4条回答
  •  独厮守ぢ
    2020-12-04 11:03

    Your script works if you do this:

    #!/usr/bin/perl -w
    
    binmode STDOUT, ':utf8';
    
    while(<>){
        binmode ARGV, ':utf8';
    
        my @chars = split //, $_;
        print "$_\n" foreach(@chars);
    }
    

    The magic filehandle that <> reads from is called *ARGV, and it is opened when you call readline.

    But really, I am a fan of explicitly using Encode::decode and Encode::encode when appropriate.

提交回复
热议问题