perlmonkdr has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm again, :-), i can believe that one step is one problem to me, well i was tried to create a directory with utf8 name encoding but can do this, i'm read some question similar in this thread 604999, can't found any solution.

Check this code, save in utf-8:
#!/usr/bin/perl use utf8; mkdir "Ïntêràtíonaliçzation"; system "mkdir Ïntêràtíonal"; open F,'>:utf8','./test.txt'; print F "Ïntêràtíonaliçzation"; close F;

If you see the test.txt file the word is ok encoded, but both makes dir not at least in win xp.

How can i tell to perl that create the directory in utf8

Many thank

Replies are listed 'Best First'.
Re: Mkdir and utf8
by moritz (Cardinal) on Nov 07, 2007 at 18:36 UTC
    Try this:
    use utf8; use Encode qw/encode/; mkdir encode('utf-8', "Ïntêràtíonaliçzation");

    (works for me on linux)

      Thk U moritz, is the second time that you help me, in this case this example not works on windows, if you need, check the response below, that works ok.

      Thk U again

Re: Mkdir and utf8
by ikegami (Patriarch) on Nov 07, 2007 at 21:05 UTC

    Windows has two set of system calls: "A"NSI and "W"ide.

    Perl's file functions use the "A" version of the system call, so the UTF-8 encoding of the string you pass to them is interpreted as being encoded according to the system's code page (e.g. American systems typically use cp1252, Microsoft's version of iso-latin-1).

    One way to achieve your goal is to encode the file name according to the system's code page.

    #!/usr/bin/perl use utf8; use Encode qw( encode ); use Win32::Codepage; mkdir encode(Win32::Codepage::get_encoding(), 'Ïntêràtíonaliçzation');

    That method assumes the characters you are using are present in your system's code page. If they are not, you will need to use the "W" version of the function. The "W" function accepts UCS-2le strings which can encode common UNICODE characters. Unfortunately, I don't think there currently exists a module that gives access to those functions, but you could access them via Win32::API.

      You right, the example that you give to us works exactly as you say on windows,i'm really not know that windows has two systems calls, i appreciate very much, the problem was finaly solved, i will also investigate the wide call.

      Thk U very much ikegami

        For example, the system call in question is CreateDirectory.

        BOOL WINAPI CreateDirectory( LPCTSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes );

        If you read down that page, you'll see that it's "implemented as CreateDirectoryW (Unicode) and CreateDirectoryA (ANSI)". CreateDirectory is really just an alias for one of these two actual KERNEL32 functions. If you look in winbase.h, you'll find

        WINBASEAPI BOOL WINAPI CreateDirectoryA( LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes ); WINBASEAPI BOOL WINAPI CreateDirectoryW( LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes ); #ifdef UNICODE #define CreateDirectory CreateDirectoryW #else #define CreateDirectory CreateDirectoryA #endif
        Win32API::File - Low-level access to Win32 system API calls for files/dirs.
Re: Mkdir and utf8
by apl (Monsignor) on Nov 07, 2007 at 18:22 UTC
    Please, please, please test your returns!

    mkdir "Ïntêràtíonaliçzation" || die "mkdir failed $1";

    Ditto your open...

      You might find your solution (mkdir("Ïntêràtíonaliçzation" || die "mkdir failed $1")) less than useful. Fix:

      mkdir "Ïntêràtíonaliçzation" or die "mkdir failed $!";

        You are in all details!!, really thank and your votes were given. :-)

      Thk apl, perhaps i'm not be clear, usually it happens :-), the problem exists on character set, not in a fail on create dirs, anyway thank for your concern

        No, you were clear. I was anal-retentive, but as ikegami pointed out, incorrectly so.