in reply to Re: Problems Opening file with Perl where path has UTF8
in thread Problems Opening file with Perl where path has UTF8

Well after a few days of research here is what I came up with. Thanks for all of your help and pointing me in the right direction. The main point that I learned was to get unicode to work in filenames and paths you need to get platform specific. It would be great if that was not the case. Here is the code that I got working. It is running on Windows XP with Perl 5.8.8 (from Active State). Working script:

#!/usr/bin/perl use strict; use Win32API::File qw( :ALL ); use utf8; use Encode; #Notice the \0 at the end of the file name. Necessary but I don't kno +w why. my $win32_handle = Win32API::File::CreateFileW(Encode::encode("UTF-16L +E", "C:\\test\\" . "
一棵高树
" . "\\test.txt\0"), FILE_READ_DATA, 0, [], OPEN_EXISTING, 0, []); my $perl_handle = 0; #This translates the win32 file handle to a Perl file handle Win32API::File::OsFHandleOpen($perl_handle, $win32_handle, "r"); print <$perl_handle>; if ($^E) { print "Error: " . $^E }

Replies are listed 'Best First'.
Re^3: Problems Opening file with Perl where path has UTF8
by ikegami (Patriarch) on Nov 24, 2006 at 21:41 UTC
    You shouldn't rely on $^E to tell you there's been an error. You should only use $^E when you know an error has occured.

    The nul is necessary for the system to know where the string ends. Perl will automatically add an ASCII nul (1 byte) when converting from an SV* to a char*, but an UTF16LE nul (2 bytes) is needed here.