How can I open a Unicode file with Perl?

后端 未结 4 1322
广开言路
广开言路 2021-01-04 17:58

I\'m using osql to run several sql scripts against a database and then I need to look at the results file to check if any errors occurred. The problem is that Perl doesn\'t

4条回答
  •  悲&欢浪女
    2021-01-04 18:33

    The file is presumably in UCS2-LE (or UTF-16 format).

    C:\Temp> notepad test.txt
    
    C:\Temp> xxd test.txt
    0000000: fffe 5400 6800 6900 7300 2000 6900 7300  ..T.h.i.s. .i.s.
    0000010: 2000 6100 2000 6600 6900 6c00 6500 2e00   .a. .f.i.l.e...

    When opening such file for reading, you need to specify the encoding:

    #!/usr/bin/perl
    
    use strict; use warnings;
    
    my ($infile) = @ARGV;
    
    open my $in, '<:encoding(UCS-2le)', $infile
        or die "Cannot open '$infile': $!";
    

    Note that the fffe at the beginning is the BOM.

提交回复
热议问题