[Mpls-pm] use open qw(:std :utf8); messes up readdir()?

Gerd Knops gerti-pm at bitart.com
Wed Apr 20 11:34:35 PDT 2011


Below a small sample illustrating the problem.

As is, the newly written file will show up with a garbled file name in the directory listing, but when printing the file contents the umlauts look fine.

When the "use open" line is commented out, the directory listing looks right, but of course printing the file content looks wrong.

Only workaround I have found so far is to add "use Encode;" and in the dir listing loop add

	fileName=decode('utf8',$fileName);

Gerd


#!/usr/bin/perl -w

use open qw(:std :utf8);
use utf8;

my $umlauts='äöüß';

open(OUT,">$umlauts") or die("Can't open '$umlauts' for write: $!\n");
print OUT "$umlauts\n";
close(OUT);

opendir(DIR,'.') or die("Can't read directory '.': $!\n");
foreach my $fileName (readdir(DIR))
{
	print "-> $fileName\n";
}
closedir(DIR);

open(IN,"$umlauts") or die("Can't open '$umlauts' for read: $!\n");
while(<IN>)
{
	print "$umlauts: $_";
}
close(IN);



More information about the Mpls-pm mailing list