Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create unicode file names in Windows using Perl

I have the following code

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

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

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.

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

like image 441
Archimedes Trajano Avatar asked May 13 '11 13:05

Archimedes Trajano


1 Answers

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

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 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's support for Windows sucks in some respects.

like image 76
ikegami Avatar answered Sep 22 '22 14:09

ikegami