如何在 Perl 中检查 Windows 上是否存在 Unicode 目录?

发布于 2024-08-19 23:21:02 字数 444 浏览 3 评论 0原文

我需要检查 Perl 中是否存在 Unicode 目录。我使用的是 Windows XP 和 Perl Camelbox 5.10.0。

如果我尝试创建一个目录(就像此处建议的 Sinan stackoverflow.com/questions/2184726)已经存在的程序会死掉。

不幸的是 if ( !-d $dir_name ) { # create directory $dir_name } 似乎无法识别 Unicode 目录,或者我正在做一些完全愚蠢的事情。我尝试在检查之前对目录名称进行编码,但结果是相同的。

如何检查 Unicode 目录是否存在?

I need to check whether a Unicode directory exists in Perl. I am using Windows XP and Perl Camelbox 5.10.0.

If I try to create a directory (like Sinan suggested here stackoverflow.com/questions/2184726) that already exists the program dies.

Unfortunately if ( !-d $dir_name ) { # create directory $dir_name } doesn't seem to recognize Unicode directories or I am doing something completely stupid. I tried to encode the directory name before checking it, but the result is the same.

How do I check for the existance of a Unicode directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2024-08-26 23:21:02

在回答您之前的问题时,我忘记了 Win32.pm 提供了一个不错的界面。我会回去回答。但是,对于您眼前的问题,您需要做的不是当 CreateDirectory 调用失败时自动 die,而是检查 错误代码。如果错误代码是0xb7 (ERROR_ALREADY_EXISTS),那么您就可以继续愉快的生活了。

问题是,当您有 Unicode 文件名时,很难继续使用 Perl 函数。解决方案是使用 Win32::GetANSIPath (只需关注路径的完整长度):

#!/usr/bin/perl

use strict; use warnings;
use utf8;

use Encode qw( encode );
use File::Slurp;
use File::Spec::Functions qw( catfile );
use Win32;
use Win32::API;

use constant ERROR_ALREADY_EXISTS => 0xb7;

my $dir_name = 'Волгогра́д';

unless ( Win32::CreateDirectory($dir_name) ) {
    my $err = $^E;
    if ( $err == ERROR_ALREADY_EXISTS ) {
        warn "Directory exists, no problem\n";
    }
    else {
        die Win32::FormatMessage($^E);
    }
}

my $ansi_path = Win32::GetANSIPathName($dir_name);
warn "$ansi_path\n";

哦,还有,祝你删除该目录好运。

不过,严肃地说,整个 Windows Unicode 文件操作有点混乱。

据我了解,如果您希望能够使用 Perl 函数(例如 open)来处理包含 Unicode 字符的路径,则需要 ANSI 路径名。例如:

my $file = catfile($dir_name, 'test.txt');

open my $fh, '>', $file
    or die "cannot create '$file': $!";

会失败而

my $file = catfile($ansi_path, 'test.txt');

open my $fh, '>', $file
    or die "cannot create '$file': $!";

会成功(至少在我的系统上)。如果您只想使用 Win32 API 函数来处理文件,则不需要 ANSI 路径(这在您的情况下可能更容易)。 CPAN 上有很多模块可以帮助您完成后者。

When answering your earlier question, I forgot that Win32.pm provides a decent interface. I will go back and that answer. However, for your immediate problem, what you need to do is not to automatically die when the CreateDirectory call fails, but to check the error code. If the error code is 0xb7 (ERROR_ALREADY_EXISTS), you go on your merry way.

The problem is that it is hard to go on your merry way using Perl functions when you have a Unicode file name. The solution is to use Win32::GetANSIPath (just keep an eye on the full length of the path):

#!/usr/bin/perl

use strict; use warnings;
use utf8;

use Encode qw( encode );
use File::Slurp;
use File::Spec::Functions qw( catfile );
use Win32;
use Win32::API;

use constant ERROR_ALREADY_EXISTS => 0xb7;

my $dir_name = 'Волгогра́д';

unless ( Win32::CreateDirectory($dir_name) ) {
    my $err = $^E;
    if ( $err == ERROR_ALREADY_EXISTS ) {
        warn "Directory exists, no problem\n";
    }
    else {
        die Win32::FormatMessage($^E);
    }
}

my $ansi_path = Win32::GetANSIPathName($dir_name);
warn "$ansi_path\n";

Oh, and, good luck deleting that directory.

In a serious vein, though, the whole Windows Unicode file operations thing is a bit of a mess.

As far as I understand these things, you need the ANSI path name if you want to be able to use Perl functions such as open to work with paths containing Unicode characters. E.g.:

my $file = catfile($dir_name, 'test.txt');

open my $fh, '>', $file
    or die "cannot create '$file': $!";

will fail whereas

my $file = catfile($ansi_path, 'test.txt');

open my $fh, '>', $file
    or die "cannot create '$file': $!";

will succeed (at least on my system). You do not need the ANSI paths if you are going to only use Win32 API functions to deal with files (and that might be easier in your case). There are a bunch of modules to help you with the latter on CPAN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文