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

Can any one shed any light on an issue I am having with Tk getOpenFile. I can open 99% percent of files by calling this, however I can not get any file names with a '£' to open.

The file will open with a standard call to open passed by readdir, but not with getOpenFile. I also notice that getOpenFile translates the '£' to a 'u' which Im sure is whats causing the issue

A small Tk app shows the problem.
use Tk; use strict; my $mw = new MainWindow(); &getfile; MainLoop(); sub getfile{ my $filename = $mw->getOpenFile(); if(-f $filename){ open (FILE,"<",$filename) || die "unable to open $filename $!\n"; close (FILE); die "FILE OPENED -> $filename\n"; }else{ die $!; } }

I am running Active State 5.8 on Win XP

thanks: Skywalker

Replies are listed 'Best First'.
Re: opening files with tk getOpenFile
by Marshall (Canon) on May 08, 2009 at 14:45 UTC
    I don't have personal experience with this but it appears that you need wide character filenames (normal encoding doesn't have the "pound sterling" symbol. You might find this helpful..
    http://aspn.activestate.com/ASPN/Mail/Message/perl-win32-users/3171931
    although it sounds like you are going to have do some work. You might want to also look into Perl 5.10 to see if somehow this got better. Sorry I can't help more. Good luck!
Re: opening files with tk getOpenFile
by zentara (Cardinal) on May 08, 2009 at 17:20 UTC
    Try this trick graff showed me. It somehow forces Perl to set the right flag on the filename to make it decode right.
    #this decode utf8 routine is used so filenames with extended # ascii characters (unicode) in filenames, will work properly use Encode; opendir my $dh, $path or warn "Error: $!"; my @files = grep !/^\.\.?$/, readdir $dh; closedir $dh; # @files = map{ "$path/".$_ } sort @files; #$_ = decode( 'utf8', $_ ) for ( @files ); @files = map { decode( 'utf8', "$path/".$_ ) } sort @files;
    HTH, zentara

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: opening files with tk getOpenFile
by John M. Dlugosz (Monsignor) on May 09, 2009 at 00:17 UTC
    ActiveState decided it was better to use the ANSI APIs for files. However, the Unicode version still exists, they just disabled respecting that flag. Recompile yourself and you can use file names in all their glory. I had to do that to make a backup program keep working after I got a new version from AS.

    The File API's in Windows don't even use the ANSI code page (e.g. Latin-1). They use the "OEM" code page, like DOS did. So some characters encode differently. The '£' in Windows 1252 A3 (and this is different from ISO 8859-1). That byte in Code Page 437 is the LATIN SMALL LETTER U WITH ACUTE, "ú". So Perl converts the internal string to code page 1252 (if it was of the Unicode persuasion) and codes an A3 byte where you put '£'. Then it calls the Windows API function ending with -A, which is taking its argument in code page 437, and thinks the A3 byte is a 'ú', which it converts into UTF-16LE (U+00FA) to pass to the -W version of that function, which is unambiguously Unicode and is certain you meant 'ú'.

    You can also call a Windows function SetFileApisToANSI, using the Win32 module to call the Kernel32 DLL, and see if that helps. At least it removes this layer of obfuscation.

    —John

      Thanks for the responses ill have a play around.