in reply to Tk causes problems with file paths containing non-Latin-1 chars

This should work on Win32:

use warnings; use strict; use Tk; if ($^O =~ /Win32/){ my $gof_wrapped = \&Tk::getOpenFile; *Tk::getOpenFile = sub{my $file = shift->$gof_wrapped; return Win32::GetANSIPathName($file)}; } my $mw = tkinit; my $path = $mw->getOpenFile(); open (my $fh, '<', $path) or die "can not open file $path : $!"; while (my $line = <$fh>) { print $line; } close $fh; MainLoop;
  • Comment on Re: Tk causes problems with file paths containing non-Latin-1 chars
  • Download Code

Replies are listed 'Best First'.
Re^2: Tk causes problems with file paths containing non-Latin-1 chars
by ron7 (Beadle) on May 04, 2011 at 23:57 UTC
    Thanks! That fixes the problem in my test code. Refactoring the app will be a bit more complex, but it looks do-able.

    The changes to the test program so it will run under Linux|OS-X|Win32 are:

    ... use if ($^O =~ m/MSWin32/i || ''), 'Win32'; ... my $is_win32 = $^O =~ m/MSWIN32/i; ... sub test_file { my $file = ucx(shift); ... } sub ucx { my $fn = shift; return $is_win32 ? Win32::GetANSIPathName($fn) : decode('utf8', $fn) +; }

    Out of curiosity, I went looking for the GetANSIPathName method in the ActiveState (win32) lib code. As far as I could see, it's not a Perl function, so I guess it must be all in a dll someplace.

    For anyone who wants to play with this, a zip of the code and data can be downloaded from http://itee.uq.edu.au/~chernich/downloads/tkbug.zip.