Question

Is the CWD module regarding Unicode not up to date or is abs_path supposed only to be used when writing to the 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;

output:

# /tmp/Hello âº
# /tmp/Hello ☺
Was it helpful?

Solution

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);

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top