Perl v. 5.8.6, OS X 10.4.7:

#!/usr/bin/perl -w
BEGIN { $^W = 1 }
use strict;
# use open ':utf8';
my $of = "index.txt";
open OF, "> $of"
        or die "cannot create `$of': $!";
opendir(DIR, '.') or die 'cannot open : $!';
my @file = readdir(DIR);
closedir(DIR);
for my $file (sort @file) {
        printf OF "$file\n";
}

Running this, the file names get written correctly to OF as UTF-8. But *uncommenting* the line

        # use open ':utf8';

gives garbled UTF-8 in the OF.

A fix is to

        use Encode;

and then

        my @file = map { decode("utf8", $_) } readdir(DIR);

makes things work out OK, even uncommenting the line

        use open ':utf8';

But, using decode() shouldn't be necessary, right?


Reply via email to