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

Dear Monks

I am really struggling since days with unicode directory names, for example "Małretuzz" (both Activestate and Strawberry, W7). What is wrong with this?

use utf8; use Win32; $dir="Małretuzz"; print our $short_path_dir = Win32::GetShortPathName( "$dir" );

This gives me a not initialized value.

What I am trying to do is to get a directory name (and path) from a Tk GUI. In this directory I'll create a sqlite database. If the directory contains not english characters...it doesn't work. I've search Perlmonks, I've found suggestions with different Win32s, nothing work here...

use utf8; use Win32; use Tk; use File::Spec; my $dir = File::Spec->canonpath( $mw->chooseDirectory() ); my $short_path_dir=Win32::GetANSIPathName($dir); #alternative #my $short_path_dir = Win32::GetShortPathName( "$dir" ); my $path_and_database="$short_path_dir" . "\\DATABASE\.db"; my $dbh = DBI->connect("dbi:SQLite:$path_and_database","","", {}); $dbh->do( "CREATE TABLE table( ID INTEGER PRIMARY KEY, POS, simple)" ) +; $dbh->disconnect;

Replies are listed 'Best First'.
Re: directory unicode
by kcott (Archbishop) on Jun 26, 2013 at 00:43 UTC

    Within <c>...</c> and <code>...</code> tags, Unicode characters (outside the ASCII range) are displayed as character references (e.g. &#322;). In these cases, it's better to use <pre>...</pre> blocks (or <tt>...</tt> for inclusion within a sentence). The code you posted looks like '$dir="Ma&#322;retuzz";' but it should look like '$dir="Małretuzz";': we have no way of knowing which of those forms you typed either in your posting or in your original code. [Side issue: while it probably doesn't matter in this instance, given that you're not intending any interpolation of Małretuzz, single quotes would be more appropriate.]

    This line of code looks highly dodgy (although B::Deparse reports no syntax errors1):

    print our $short_path_dir = Win32::GetShortPathName( "$dir" );

    Something like this would probably have been better:

    my $short_path_dir = Win32::GetShortPathName($dir); print $short_path_dir;

    The actual error returned would have been better than a prosaic description of it (i.e. "This gives me a not initialized value."). Win32::GetShortPathName(PATHNAME) says "Returns undef when the PATHNAME does not exist.", so that seems to be a likely candidate: the cause may be related to how you typed Małretuzz or you may be just running your script in a directory that does not have a Małretuzz subdirectory. Furthermore, using warnings and strict will alert you to other potential issues in your code.

    [1] "... B::Deparse reports no syntax errors"

    $ perl -MO=Deparse,p -e 'print our $short_path_dir = Win32::GetShortPa +thName( "$dir" );' print our $short_path_dir = Win32::GetShortPathName("$dir"); -e syntax OK

    -- Ken

Re: directory unicode ( not Tk->chooseDirectory , Win32::Unicode )
by Anonymous Monk on Jun 26, 2013 at 02:27 UTC