英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论