perl-unicode

Re: Perl and unicode file names

2005-02-24 08:35:25
Peter Gordon wrote:
Hi Guys.

I need some help with a project that I have. I have to copy files using
Perl to different places and the filenames may be in Hebrew, Chinese,
Korean etc.
The problem is, that filenames, when using opendir, are returned as
question marks. In the DOS box I have set the codepage to 862. So DIR
returns accented characters, but Perl still returns question marks. I
have also set "use utf8", but that didn't help either.

So the problem I have is how to proceed. Should I give up with Perl and
use Java or C? Any suggestions gratefully received.

I don't think you have to give up using Perl.

Something like this should work:

#!/usr/bin/perl

use strict;
use warnings;

use Encode;
use IO::Dir;

# Let perl know that we want to output cp862 on STDOUT
binmode( STDOUT, ':encoding(cp862)' );

my $dir = IO::Dir->new('.')
  or die("Failed to open dir : $!");

while ( my $entry = $dir->read ) {

  next if $entry =~ /^\.{1,2}$/;

  # Decode octets into perl's internal unicode encoding
  $entry = Encode::decode_utf8($entry, 1);

  printf( "%s\n", $entry );
}

$dir->close;


Regards
Christian



<Prev in Thread] Current Thread [Next in Thread>