为什么对于包含非 ASCII 字符的文件名,-e 文件存在性测试总是 return false?
Why does the -e file existence test always return false for a filename containing non-ASCII characters?
我正在使用 Perl 检查名称中包含非 ASCII 字符的文件是否存在。即使该文件存在,检查始终 returns 错误。我在 Windows 10 机器上使用 Strawberry Perl v5.24.0。
这是我的代码:
use strict;
use warnings;
use Encode;
my $file = '<SOME DIR PATH> / áéíóú.mov';
if (-e $file) {
print "$file exists";
} else {
print " $file does not exists" ;
}
我还通过 运行 chcp 65001
更改了 cmd shell 中的代码页。然后 cmd 能够识别这些字符,但不知何故它总是 returns "Not Exists" 这个文件。
我该如何解决这个问题?
use strict;
use warnings;
# Properly decode source code, which is expected to be UTF-8.
# This allows non-ASCII characters in the source.
use utf8;
# Properly decode text received from STDIN.
# Properly encode text sent to STDOUT and STDERR.
use Win32 qw( );
my ( $enc_in, $enc_out, $enc_syscall );
BEGIN {
$enc_input = 'cp'.Win32::GetConsoleCP();
$enc_output = 'cp'.Win32::GetConsoleOutputCP();
$enc_syscall = 'cp'.Win32::GetACP();
binmode STDIN, ":encoding($enc_input)";
binmode STDOUT, ":encoding($enc_output)";
binmode STDERR, ":encoding($enc_output)";
}
use Encode qw( encode );
my $file = 'áéíóú.mov';
if (-e encode($enc_syscall, $file, Encode::FB_CROAK | Encode::LEAVE_SRC)) {
print("$file exists\n");
}
elsif ($!{ENOENT}) {
print("$file doesn't exist\n");
}
else {
die("Can't determine if \"$file\" exists: $!\n");
}
或
use strict;
use warnings;
# Properly decode source code, which is expected to be UTF-8.
# This allows non-ASCII characters in the source.
use utf8;
# Properly decode text received from STDIN.
# Properly encode text sent to STDOUT and STDERR.
use Win32 qw( );
my ( $enc_in, $enc_out, $enc_syscall );
BEGIN {
$enc_input = 'cp'.Win32::GetConsoleCP();
$enc_output = 'cp'.Win32::GetConsoleOutputCP();
$enc_syscall = 'cp'.Win32::GetACP();
binmode STDIN, ":encoding($enc_input)";
binmode STDOUT, ":encoding($enc_output)";
binmode STDERR, ":encoding($enc_output)";
}
use Win32::Unicode::File qw( statW );
my $file = 'áéíóú.mov';
if (statW($file)) {
print("$file exists\n");
}
elsif ($!{ENOENT}) {
print("$file doesn't exist\n");
}
else {
die("Can't determine if \"$file\" exists: $^E\n");
}
后者不限于包含机器 ANSI 字符集字符的路径。
我正在使用 Perl 检查名称中包含非 ASCII 字符的文件是否存在。即使该文件存在,检查始终 returns 错误。我在 Windows 10 机器上使用 Strawberry Perl v5.24.0。
这是我的代码:
use strict;
use warnings;
use Encode;
my $file = '<SOME DIR PATH> / áéíóú.mov';
if (-e $file) {
print "$file exists";
} else {
print " $file does not exists" ;
}
我还通过 运行 chcp 65001
更改了 cmd shell 中的代码页。然后 cmd 能够识别这些字符,但不知何故它总是 returns "Not Exists" 这个文件。
我该如何解决这个问题?
use strict;
use warnings;
# Properly decode source code, which is expected to be UTF-8.
# This allows non-ASCII characters in the source.
use utf8;
# Properly decode text received from STDIN.
# Properly encode text sent to STDOUT and STDERR.
use Win32 qw( );
my ( $enc_in, $enc_out, $enc_syscall );
BEGIN {
$enc_input = 'cp'.Win32::GetConsoleCP();
$enc_output = 'cp'.Win32::GetConsoleOutputCP();
$enc_syscall = 'cp'.Win32::GetACP();
binmode STDIN, ":encoding($enc_input)";
binmode STDOUT, ":encoding($enc_output)";
binmode STDERR, ":encoding($enc_output)";
}
use Encode qw( encode );
my $file = 'áéíóú.mov';
if (-e encode($enc_syscall, $file, Encode::FB_CROAK | Encode::LEAVE_SRC)) {
print("$file exists\n");
}
elsif ($!{ENOENT}) {
print("$file doesn't exist\n");
}
else {
die("Can't determine if \"$file\" exists: $!\n");
}
或
use strict;
use warnings;
# Properly decode source code, which is expected to be UTF-8.
# This allows non-ASCII characters in the source.
use utf8;
# Properly decode text received from STDIN.
# Properly encode text sent to STDOUT and STDERR.
use Win32 qw( );
my ( $enc_in, $enc_out, $enc_syscall );
BEGIN {
$enc_input = 'cp'.Win32::GetConsoleCP();
$enc_output = 'cp'.Win32::GetConsoleOutputCP();
$enc_syscall = 'cp'.Win32::GetACP();
binmode STDIN, ":encoding($enc_input)";
binmode STDOUT, ":encoding($enc_output)";
binmode STDERR, ":encoding($enc_output)";
}
use Win32::Unicode::File qw( statW );
my $file = 'áéíóú.mov';
if (statW($file)) {
print("$file exists\n");
}
elsif ($!{ENOENT}) {
print("$file doesn't exist\n");
}
else {
die("Can't determine if \"$file\" exists: $^E\n");
}
后者不限于包含机器 ANSI 字符集字符的路径。