質問

は、UnicodeのUnicodeに関するCWD モジュール、またはabs_pathを使用するのでは、使用されるだけで使用されることを想定しています。OSに書くときは?

#!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;
binmode STDOUT, ':encoding(utf-8)';
use Cwd qw(abs_path);
use File::Spec::Functions qw(rel2abs);

chdir '/tmp';

my $file = "Hello \x{263a}";

open my $fh, '>', $file or die $!;
say $fh 'test';
close $fh;

say abs_path $file;
say rel2abs $file;
.

出力:

# /tmp/Hello âº
# /tmp/Hello ☺
.

役に立ちましたか?

解決

The Cwd module uses char * types internally and so does not handle encoding at all. Generally, filesystems do not care what characters or encoding you use for your filenames, as long as you escape any special characters (e.g. '/')

If you want to tell Perl the file path is in UTF-8, you can encode it:

use Encode qw(decode_utf8);

...
say decode_utf8(abs_path $file);

他のヒント

Perl leaves it to you to decode paths it returns and encode paths you provide. (YUCK!)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top