在Windows上处理Perl中的unicode目录和文件名

前端之家收集整理的这篇文章主要介绍了在Windows上处理Perl中的unicode目录和文件名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有Perl和 Windows的编码问题.在运行Perl的Windows 7(草莓5.16)和简单的TK GUI上,我需要打开文件和/或访问名称/路径中包含非英文字符的目录.对于打开文件我已经提出这个解决方案似乎工作正常:
#!/usr/bin/perl -w

use strict;
use warnings;
use Win32::Unicode::File;
use Encode;
use Tk;

my $mw = Tk::MainWindow->new;
my $tissue_but = $mw->Button(
    -text => 'Open file',-command =>  [ \&select_unicode_file ],);
$tissue_but->grid( -row => 3,-column => 1 );
Tk::MainLoop();

sub select_unicode_file{
my $types = [ ['Txt','.txt'],['All Files','*'],];
my $input_file= $mw->getOpenFile(-filetypes => $types);
my $fh = Win32::Unicode::File->new;
if ($fh->open('<',$input_file)){
  while (my $line = $fh->readline()){
    print "\n$line\n";
  }
   close $fh;
}
 else{
  print "Couldn't open file: $!\n";
}
}

这会正确打开Поиск/Поиск.txt等文件

我不能做的只是获取目录路径而不是处理它.我想我应该使用Win32 :: Unicode :: Dir,但我真的无法理解文档.

它应该是这样的:

#!/usr/bin/perl -w

use strict;
use warnings;
use Win32::Unicode::Dir;
use Encode;
use Tk;

my $mw = Tk::MainWindow->new;
my $tissue_but = $mw->Button(
    -text => 'Open file',-command =>  [ \&select_unicode_directory ],-column => 1 );
Tk::MainLoop();

sub select_unicode_directory{
my $dir = $mw->chooseDirectory( );
my $wdir = Win32::Unicode::Dir->new;
my $dir = $wdir->open($dir) || die $wdir->error;
my $dir_complete = "$dir/a.txt";
open (MYFILE,$dir_complete );
    while (<MYFILE>) {
    chomp;
    print "$_\n";
}
close (MYFILE); 
}
在以下方面存在逻辑错误
my $dir = $wdir->open($dir) || die $wdir->error;
my $dir_complete = "$dir/a.txt";

$wdir-> open(‘path’)返回一个对象,而不是一个字符串.你不能像路径一样使用它.但这不是最糟糕的.遗憾的是,似乎Tk实现还没有支持Unicode文件名(包括chooseDirectory).我想你将不得不写一个自定义目录选择器,但我不确定它是否可能.

这能够在ascii-chars文件夹中列出文件(和 – > fetch可以列出utf-8文件),并在打开带有utf-8字符的文件夹时崩溃.嗯,公平地说,当打开??????时崩溃.

use strict;
use warnings;
use Win32::Unicode::Dir;
use Win32::Unicode::Console;
use Encode;
use Tk;

my $mw = Tk::MainWindow->new;
my $tissue_but = $mw->Button(
    -text => 'Select dir',-column => 1 );
Tk::MainLoop();

sub select_unicode_directory {
    my $wdir = Win32::Unicode::Dir->new;
    my $selected = $mw->chooseDirectory(-parent =>$mw);
       # http://search.cpan.org/dist/Tk/pod/chooseDirectory.pod#CAVEATS
       $selected = encode("utf-8",$selected);
    print "selected: $selected\n";

    $wdir->open($selected) || die $wdir->error;

    print "\$mw->chooseDirectory:    $selected\n";
    print "\$wdir->open(\$selected): $wdir\n";


# CRASH HERE,presumably because winders can't handle '?' in a file (dir) name
    for ($wdir->fetch) {
# http://search.cpan.org/~xaicron/Win32-Unicode-0.38/lib/Win32/Unicode/Dir.pm
        next if /^\.{1,2}$/;
        my $path = "$selected/$_";
        if (file_type('f',$path)) { print "file: $path\n"; } 
        elsif (file_type('d',$path)) { print " dir: $path\n"; }
    }
    print "closing \n";
    $wdir->close || die $wdir->error;

}

取样(打开Поиск/):

下面的两个样本都使用:为MSWin32-x64多线程构建的Strawberry Perl 5.12.3

selected: C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk/?????
$mw->chooseDirectory:    C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk/?????
$wdir->open($selected): Win32::Unicode::Dir=HASH(0x2e38158)
>>> perl crash <<<

取样(开放Поиск的父母):

selected: C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk
$mw->chooseDirectory:    C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk
$wdir->open($selected): Win32::Unicode::Dir=HASH(0x2b92c10)
file: C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk/.select_uni_dir.pl.swp
file: C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk/o
file: C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk/o.dir
file: C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk/select_uni_dir.pl
file: C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk/select_uni_file.pl
 dir: C:/cygwin/home/jaroslav/tmp/so/perl/open-file-tk/Поиск

结论

Tk目录选择器返回?????而不是Поиск.在寻找在Tk中启用Unicode的方法时,我发现了这个:

http://search.cpan.org/dist/Tk/pod/UserGuide.pod#Perl/Tk_and_Unicode

(…) Unfortunately,there are still places in Perl ignorant of
Unicode. One of these places are filenames. Consequently,the file selectors
in Perl/Tk do not handle encoding of filenames properly. Currently they
suppose that filenames are in iso-8859-1 encoding,at least on Unix systems.
As soon as Perl has a concept of filename encodings,then Perl/Tk will also
implement such schemes.

所以乍一看,你想要做的事情似乎是不可能的(除非你编写或查找自定义目录选择器).实际上,这可能不是一个坏主意提交此错误,因为GUI确实显示“Поиск”,因此错误在返回值中.

猜你在找的Windows相关文章