4

I have a folder on my USA Windows 7 computer titled "フォルダ". There's a file in it called "foo.txt", and I put a few lines of text in it. I'm trying to read it, which is apparently a very sticky problem. An answer to a related question had a simple-looking method to open the file:

use utf8;
use Encode::Locale;
use Encode;

my $path = 'C:\Users\my name\Desktop\logrus_workspace\フォルダ\foo.txt';
my $new_path = encode(locale_fs => $path);
print $new_path;
open my $fh, '<', $new_path
    or die $!;

The call to open dies with Invalid argument. Any idea what specific problem that message indicates, and how I can get this unicode-named file open?

Community
  • 1
  • 1
Nate Glenn
  • 6,455
  • 8
  • 52
  • 95
  • 3
    Perl builtins such as `open` use the "(A)NSI" interface to the system. You'll need to encode the string as per your ANSI code page, which is probably impossible for those characters. That means you'll need to use the (W)ide interface, `CreateFileW`, as provided by Win32API::File. Yes, this sucks. – ikegami Aug 28 '13 at 21:28
  • (`CreateFile` is used to open existing files too. It creates a file *handle*.) – ikegami Aug 28 '13 at 21:29
  • I've now tried out a bunch of modules for this, and have settled on Win32::LongPath. The author is very responsive, and it just works. I'll be writing more detail about it later. – Nate Glenn Aug 29 '13 at 23:54

1 Answers1

2

Use Win32::Unicode::Native or Path::Class::Unicode.

daxim
  • 39,270
  • 4
  • 65
  • 132
  • `Win32::Unicode::File` works really simply but unfortunately doesn't work with Perl 5.18 :(. Path::Class::Unicode looks nice but also fails with "Invalid argument". – Nate Glenn Aug 29 '13 at 18:38
  • And by "doesn't work", I mean it doesn't install cleanly because of one tiny failed test. I hope someone is able to patch it, because it's a very nice module. – Nate Glenn Aug 29 '13 at 23:55