在Perl中读取中文文件名



File name is encoded in UTF-16LE on Windows. Windows offers two interfaces to access the file by name. The first one is the regular char inteface, which requires file name represented in a native encoding such as cp936 for Simplified Chinese. The second one is the wide char interface, which allows file name expressed in UTF-16.  Perl's built-in open() function doesn't use the wide char interface, so the non-English file name must be encoded in the local encoding before passing to open(). The Encode::Locale helps Perl programmer to determine the native encoding to use, here is an example usage:

#!/usr/bin/perl -w

use strict;
use utf8;
use Encode::Locale qw($ENCODING_LOCALE_FS);
use Encode;

printf "%s\n", $ENCODING_LOCALE_FS;
my $f1 = encode(locale_fs => '数据库日志.lg');
open(my $fh, "<", $f1) or die "Can't open file $f1 due to: $!";
while(<$fh>) {
    print;
}
close $fh;

 

 Python seems behave similar to Perl. Java is capable of taking file name represented in both unicode and native encoding.

你可能感兴趣的:(perl)