views:

249

answers:

1

I need to check whether a Unicode directory exists in Perl. I am using Windows XP and Perl Camelbox 5.10.0.

If I try to create a directory (like Sinan suggested here stackoverflow.com/questions/2184726) that already exists the program dies.

Unfortunately if ( !-d $dir_name ) { # create directory $dir_name } doesn't seem to recognize Unicode directories or I am doing something completely stupid. I tried to encode the directory name before checking it, but the result is the same.

How do I check for the existance of a Unicode directory?

+2  A: 

When answering your earlier question, I forgot that Win32.pm provides a decent interface. I will go back and that answer. However, for your immediate problem, what you need to do is not to automatically die when the CreateDirectory call fails, but to check the error code. If the error code is 0xb7 (ERROR_ALREADY_EXISTS), you go on your merry way.

The problem is that it is hard to go on your merry way using Perl functions when you have a Unicode file name. The solution is to use Win32::GetANSIPath (just keep an eye on the full length of the path):

#!/usr/bin/perl

use strict; use warnings;
use utf8;

use Encode qw( encode );
use File::Slurp;
use File::Spec::Functions qw( catfile );
use Win32;
use Win32::API;

use constant ERROR_ALREADY_EXISTS => 0xb7;

my $dir_name = 'Волгогра́д';

unless ( Win32::CreateDirectory($dir_name) ) {
    my $err = $^E;
    if ( $err == ERROR_ALREADY_EXISTS ) {
        warn "Directory exists, no problem\n";
    }
    else {
        die Win32::FormatMessage($^E);
    }
}

my $ansi_path = Win32::GetANSIPathName($dir_name);
warn "$ansi_path\n";

Oh, and, good luck deleting that directory.

In a serious vein, though, the whole Windows Unicode file operations thing is a bit of a mess.

As far as I understand these things, you need the ANSI path name if you want to be able to use Perl functions such as open to work with paths containing Unicode characters. E.g.:

my $file = catfile($dir_name, 'test.txt');

open my $fh, '>', $file
    or die "cannot create '$file': $!";

will fail whereas

my $file = catfile($ansi_path, 'test.txt');

open my $fh, '>', $file
    or die "cannot create '$file': $!";

will succeed (at least on my system). You do not need the ANSI paths if you are going to only use Win32 API functions to deal with files (and that might be easier in your case). There are a bunch of modules to help you with the latter on CPAN.

Sinan Ünür
I understand your example as far as the directory creation is concerned. But why do I need the ANSI path and what am I supposed to do with this representation? Thank you, Sinan!
Nele Kosog
@Nele see @jrockways comment at: http://stackoverflow.com/questions/2150651/perl-and-apache-on-windows-who-does-that/2150679#2150679
Sinan Ünür
Always support Unicode on Windows. ANSI is not a proper replacement. The default paths - often the only ones you can use - are localized. Worse, the My Documents path and per-user AppData will contain the username, which is Unicode. Even on English Windows you can have Chinese users, with Chinese names.
MSalters
@MSalters: Thanks for your suggestions. True - we also have users who have Unicode directory names in their path. I forgot about that.
Nele Kosog
@Sinan: I know. If it had been my choice, I wouldn't have voted for Perl on Windows. Here's the whole story ;-) "Gosh, the deadline was yesterday and we have no product - who can write a quick prototype?" When my colleague told me he had volunteered me for the job, I almost dropped dead.
Nele Kosog