How to create Unicode file names on 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 is a way to make this work the way I expected (bearing in mind that I have a Unicode file name), without using Win32 :: API, Win32API :: * or switching to another platform and using Samba to share, to change files.

The goal is to make sure that we do not have any separate Win32 modules that need to be loaded (even conditionally).

+5
source share
3 answers

Perl . "" ( ANSI).

Windows cp1252. GetACP. ( "cp" ). cp1252 .

Windows Unicode, "Wide", Perl *. Win32API:: File CreateFileW. IIRC, . , UTF-16le .

* — Perl Windows .

+5

Windows 7 Activestate Perl.

#-----------------------------------------------------------------------
# Unicode file names on Windows using Perl
# Philip R Brenan at gmail dot com, Appa Apps Ltd, 2013
#-----------------------------------------------------------------------

use feature ":5.16";
use Data::Dump qw(dump);
use Encode qw/encode decode/;
use Win32API::File qw(:ALL);

# Create a file with a unicode name

my $e  = "\x{05E7}\x{05EA}\x{05E7}\x{05D5}\x{05D5}\x{05D4}".
         "\x{002E}\x{0064}\x{0061}\x{0074}\x{0061}"; # File name in UTF-8
my $f  = encode("UTF-16LE", $e);  # Format supported by NTFS 
my $g  = eval dump($f);           # Remove UTF ness
   $g .= chr(0).chr(0);           # 0 terminate string
my $F  = Win32API::File::CreateFileW
 ($g, GENERIC_WRITE, 0, [], OPEN_ALWAYS, 0, 0); # Create file via Win32API
say $^E if $^E;                   # Write any error message

# Write to the file

OsFHandleOpen(FILE, $F, "w") or die "Cannot open file";
binmode FILE;                       
print FILE "hello there\n";       
close(FILE);
+1

Use Encode :: Locale :

use utf8;
use Encode::Locale;
use Encode;

open($file, '>:encoding(UTF-8)', encode(locale_fs => "さっちゃん.txt") ) or die $!;
print $file "さっちゃん";
0
source

All Articles