There was a P5P thread recently about encoding filename on Win32. A while back, I wrote an app that had to support Shift-JIS and other filesystem encodings transparently. I came up with the following unpleasant but successful hack.
Whenever I want to pass a filename to any system function (open, opendir, unlink, -f, etc) I wrap the filename string in a localfile()
call like so: unlink localfile("foo.txt")
. The localfile()
function is defined as follows
use Encode; use English qw(-no_match_vars);
my $encoding; sub localfile { my ($filename) = @_; if (!defined $encoding) { $encoding = q{}; if ($OSNAME eq 'MSWin32') { require Win32::Codepage; $encoding = Win32::Codepage::get_encoding() || q{}; $encoding &&= Encode::resolve_alias($encoding) || q{}; } } return $encoding ? encode($encoding, $filename) : $filename; }