如何使用 Perl 在 Windows 中创建 unicode 文件名 [英] How do you create unicode file names in Windows using Perl

查看:40
本文介绍了如何使用 Perl 在 Windows 中创建 unicode 文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

use utf8;
open($file, '>:encoding(UTF-8)', "さっちゃん.txt") or die $!;
print $file "さっちゃん";

但我得到的文件名为 ã•ã£ã¡ã‚ƒã‚.txt

But I get the file name as ã•ã£ã¡ã‚ƒã‚".txt

我想知道是否有一种方法可以使这项工作如我所料(意味着我有一个 unicode 文件名),而无需求助于 Win32::API、Win32API::* 或移动到另一个平台并使用 Samba共享以修改文件.

I was wondering if there was a way of making this work as I would expect (meaning I have a unicode file name) this without resorting to Win32::API, Win32API::* or moving to another platform and using a Samba share to modify the files.

目的是确保我们没有任何需要加载(即使有条件)的 Win32 特定模块.

The intent is to ensure we do not have any Win32 specific modules that need to be loaded (even conditionally).

推荐答案

Perl 将文件名视为不透明的字节字符串.它们需要根据您的语言环境"的编码(ANSI 代码页)进行编码.

Perl treats file names as opaque strings of bytes. They need to be encoded as per your "locale"'s encoding (ANSI code page).

在 Windows 中,这通常是 cp1252.它由 GetACP 系统调用返回.(前置cp").但是,cp1252 不支持日语字符.

In Windows, this is is usually cp1252. It is returned by the GetACP system call. (Prepend "cp"). However, cp1252 doesn't support Japanese characters.

Windows 还提供了Unicode"又称为Wide"接口,但 Perl 不提供使用内置 * 的访问权限.不过,您可以使用 Win32API::File 的 CreateFileW.IIRC,你还需要自己对文件名进行编码.如果是这样,您将使用 UTF-16le 作为编码.

Windows also provides a "Unicode" aka "Wide" interface, but Perl doesn't provide access to it using builtins*. You can use Win32API::File's CreateFileW, though. IIRC, you need to still need to encode the file name yourself. If so, you'd use UTF-16le as the encoding.

* —Perl 对 Windows 的支持在某些方面很糟糕.

* — Perl's support for Windows sucks in some respects.

这篇关于如何使用 Perl 在 Windows 中创建 unicode 文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆