Perl的GetFiles函数在使用MBCS(日文)字符时返回问号。

huangapple go评论69阅读模式
英文:

Perl GetFiles function with MBCS (Japanese) characters returns question marks

问题

I have this code below and it returns "C:?????.log" instead of "C:\あいうえお.log" which is what I expected.

#!/usr/bin/perl

use strict;
use utf8;
use Win32::Clipboard;
use Encode;

my $clipboard = Win32::Clipboard();
my @files = $clipboard->GetFiles();

foreach my $f (@files) {
    print $f;
}

What I did was that I created a text file "C:\あいうえお.log”. I pressed Ctrl+C on a Windows Explorer on Windows 11 environment after selecting the log file. I opened a Dos prompt and went to the location where this perl script above is saved and run it. I get "C:?????.log" instead of "C:\あいうえお.log”。 I have Japanese fonts installed so when I do "dir c:". The log file is correctly listed without any character corruption.

When I tried to write a similar script with $clipboard->GetText() instead of GetFiles(). After copying Japanese text, it printed it correctly. It looks like "GetFiles" are not handling Japanese file names correctly. This GetFiles script correctly worked in Windows 10 environment.

I tried encoding STDOUT or output of GetFiles but that didn't do anything.

英文:

I have this code below and it returns "C:?????.log" instead of "C:\あいうえお.log" which is what I expected.

#!/usr/bin/perl

use strict;
use utf8;
use Win32::Clipboard;
use Encode;

my $clipboard = Win32::Clipboard();
my @files = $clipboard->GetFiles();

foreach my $f (@files) {
    print $f;
}

What I did was that I created a text file "C:\あいうえお.log”. I pressed Ctrl+C on a Windows Explorer on Windows 11 environment after selecting the log file. I opened a Dos prompt and went to the location where this perl script above is saved and run it. I get "C:?????.log" instead of "C:\あいうえお.log”。 I have Japanese fonts installed so when I do "dir c:". The log file is correctly listed without any character corruption.

When I tried to write a similar script with $clipboard->GetText() instead of GetFiles(). After copying Japanese text, it printed it correctly. It looks like "GetFiles" are not handling Japanese file names correctly. This GetFiles script correctly worked in Windows 10 environment.

I tried encoding STDOUT or output of GetFiles but that didn't do anything.

答案1

得分: 2

I think you may have to use the GetAsFormat method with Win32::Clipboard to tell it to encode as Unicode.

I tried something like this and it copied my Unicode text correctly:

#!/usr/bin/perl

use strict;
use utf8;
use Win32::Clipboard;
use Encode;

my $clipboard = Win32::Clipboard();
my $files = $clipboard->GetAs(CF_UNICODETEXT) || die 'Something went wrong!';

foreach my $f (split /\s/, $files) {
    print $f;
}
英文:

I think you may have to use the GetAsFormat method with Win32::Clipboard to tell it to encode as Unicode.

I tried something like this and it copied my Unicode text correctly:

#!/usr/bin/perl

use strict;
use utf8;
use Win32::Clipboard;
use Encode;

my $clipboard = Win32::Clipboard();
my $files = $clipboard->GetAs(CF_UNICODETEXT) || die 'Something went wrong!';

foreach my $f (split /\s/, $files) {
    print $f;
}

huangapple
  • 本文由 发表于 2023年4月4日 07:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定