Hello Monks
As always in my relationship to Perl and Windows, I am having problems with non Latin characters in file names/directories. In this case I am selecting an Excel file in Tk with getOpenFile. This seems to work fine, as the script demonstrate. On my machine (Windows 10, locale German), I can open Excel files with file names and in directories with non Latin characters (Chinese and so on). The selected path is shown correctly in my Tk app. However, I can not parse it as it fails with Tk::Error: Can't call method "read" on an undefined value at C:/Perl/site/lib/Spreadsheet/ParseXLSX.pm line 79. which means something is wrong with the path. As you can see, I try to use also Win32::LongPath: in some case it gives back a long path which is then fine for Spreadsheet::ParseXLSX, in others it gives back the original, which Spreadsheet::ParseXLSX can not open. Note that the script has to run on any desktop machine having all different locale settings.
use strict;
use warnings;
use Tk;
use Win32::LongPath;
use Spreadsheet::ParseXLSX;
my $mw = Tk::MainWindow->new();
my $path;
my $button = $mw->Button(
-text => "Select a file",
-command => \&show_file_dialog,
)->pack(-side => 'left',);
my $label = $mw->Label(
-text => 'No file yet',
)->pack(-side => 'left',);
$mw->MainLoop();
sub show_file_dialog {
my @ext = (
["Excel", ['xlsx']],
["All files", ['*']],
);
$path = $mw->getOpenFile(
-filetypes => \@ext,
);
#use shortpath
my $ShortPath = shortpathL ($path);
#print path read and path converted with short path inside Tk
$label->configure(-text => "$path - $ShortPath");
print "try to open file with shortpathL path\n";
my $parser = Spreadsheet::ParseXLSX->new();
my $workbook = $parser->parse($ShortPath);
print "try to open file with original path\n";
my $parser = Spreadsheet::ParseXLSX->new();
my $workbook = $parser->parse($path);
}
What can I do to achieve my goal given that shortpathL returns only sometimes a nice path? (for example it dowsn't seem to work if the file is on a removable device, or in some positions on the hard drive, etc.). Win32::Unicode is not an option as it is not maintained and compilation fails on any modern Perl. Thank you for your suggestions.