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

Note: I'm assuming you have the actual characters 一棵高树 in there instead of 一棵高树

Try

use Encode qw( decode ); my $strPath = decode("C:\\server\\htdocs\\DEVELOPMENT\\testing\\...\\t +est\\test.ssi");

or

use utf8; my $strPath = "C:\\server\\htdocs\\DEVELOPMENT\\testing\\...\\test\\te +st.ssi";

(Use the characters instead of ...)

They should both work or both fail, probably the latter. If it works, I'll explain why.

Replies are listed 'Best First'.
Re^2: Problems Opening file with Perl where path has UTF8
by lustyx (Novice) on Nov 24, 2006 at 20:49 UTC
    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 }
      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.