perl如何用UTF-8的字符集来写文件,谢谢

有人知道么 ,谢谢
CODE中如果出现utf8字符, 那么使用use utf8;
否则, 输入会根据环境来判断: Windows默认下输入到perl应该先decode(gbk,...)来转化成perl内置的utf8格式(或者gb2312也行, 没gbk全); 再通过encode(gbk, ..)将内置utf8转化成gbk输出

论坛上有很多详的讲解, 搜吧.

周6准备飞回家过年咯^_^
Happy Chinese Lunar New Year!

open( OUT, ">:utf8", "a.txt" ) or die "a.out: $!";
print OUT "\x{feff}";
print OUT "aaaa\n";
close OUT;
To @zheng 这种方法我试过,输出的中文是乱码,
To Lonki,你好,能不能给我说详细一些谢谢,我的情况是输入数据是从一个excel里读出来的,字符不限语言,有可能是英文,中文,日文,阿拉伯文等,我要生成一个text文件,这个text文件必须用utf8格式的,以便另一个程序可以用来作为输入,我贴一下code,麻烦帮我看一下,谢谢

#!usr/bin/perl -w

use strict;
use Cwd;
use Getopt::Std;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

#use Encode;

#$opt_p  local excel path
#$opt_c  column index
use vars qw($opt_x $opt_y);
getopts('x:y:');

$Win32::OLE::Warn = 3;

my $fullpath = undef;
my $columnindex = undef;

if($opt_x)
{
    $fullpath = $opt_x
}
else
{
    my $dir = getcwd;
    $fullpath = "$dir/summerloc.xls";
}

if($opt_y)
{
    $columnindex = $opt_y;
}
else
{
    $columnindex = 3;
}

$columnindex = $columnindex*1;
print "columnindex is  $columnindex\n";

$ARGV[0] = $fullpath;
$ARGV[1] = "SummerLoc.conf";


my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');

$Excel->{DisplayAlerts}=0;

print "ARGV[0] is $ARGV[0]\n";
#打开excle输入文件
my $Book = $Excel->Workbooks->Open($ARGV[0]);

my $GlobalSheet = $Book->Worksheets(1);
my $YahooSheet = $Book->Worksheets(2);
my $MsnSheet = $Book->Worksheets(3);
my $OCSheet = $Book->Worksheets(4);
my $MenuResSheet = $Book->Worksheets(5);

#:encoding(utf-打开输出文件
open(FOUT, '>:utf8', $ARGV[1]) or die "Cannot open $ARGV[1]: $!\n";

print FOUT "//NOTE: This file and info was generated by script. DO NOT modify it directly.\n";
print FOUT "//NeuStar Resource config file\n";
print FOUT "//Global Resource start\n";

#解析excel的每一个sheet,把内容读出,写入FOUT,
&arseSheet($GlobalSheet);

print FOUT "\n";
print FOUT "\n";
print FOUT "//Yahoo Resource start\n";

&arseSheet($YahooSheet);

print FOUT "\n";
print FOUT "\n";
print FOUT "//Msn Resource start\n";

&arseSheet($MsnSheet);


print FOUT "\n";
print FOUT "\n";
print FOUT "//OC Resource start\n";

&arseSheet($OCSheet);

print FOUT "\n";
print FOUT "\n";
print FOUT "//Menu Resource start\n";

&arseSheet($MenuResSheet);

close FOUT;
$Book->Close;

undef $GlobalSheet;
undef $YahooSheet;
undef $MsnSheet;
undef $OCSheet;
undef $MenuResSheet;
undef $Book;
undef $Excel;

sub ParseSheet()
{
    my $sheet = $_[0];
    my $key = undef;
    my $value = undef;
    foreach my $row (2..$sheet->UsedRange->Rows->{'Count'})
    {
        $key = $sheet->Cells($row,1)->{'Value'};
        if(defined($key))
        {
                  chomp($key);
#                  $key=encode("utf8",$key);
                  print FOUT "$key = ";
        }
        else
        {
            last;
        }
        
        $value = $sheet->Cells($row,$columnindex)->{'Value'};
        
        if(defined($value))
        {
            chomp($value);
#            $value=encode("utf8",$value);
            print FOUT "$value\n";
        }
        else
        {
            print FOUT "\n";
        }
    }
}

谢谢
我写了段测试代码从excel中读出中文然后以utf-8的格式写入txt文件中,希望对你有用!

#!usr/bin/perl -w


use strict;
use Encode;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

$Win32::OLE::Warn = 3;

my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
$Excel->{DisplayAlerts}=0;

my $Book = $Excel->Workbooks->Open('f:\Downloads\perl\test.xls');
my $Sheet = $Book->Worksheets('Sheet1');


open( OUT, ">:utf8", "a.txt" ) or die "a.out: $!";
print OUT "\x{feff}";


my $value = $Sheet->Range("A1:C3")->value;
my @tmp = @$value;
for( my $i=0 ; $i < 3 ; $i++ ){
        for( my $j = 0 ; $j < 3 ; $j++ ){
            my $test=${$tmp[$i]}[$j];
            print "\nTest1===$test\n";
            $test=decode("gbk",$test);
          print OUT $test;
          print "\t";
        }
        print "\n";
}


close OUT;
$Book->Close;


@zheng你好,
$test=decode("gbk",$test);
这个是从gbk到utf8的转换把,我的excel表里的内如可能是不限语言的,有可能是英文,中文,日文,阿拉伯文等,如果操作呢,谢谢
虽然print时做了encode, 但在读取时没做任何decode操作.


use encoding 'UTF-8', STDIN =>'UTF-8', STDOUT =>'UTF-8';
后面就不用再指明encode/decode了
lonki你好,我的输入文件是excel文件,我应该怎么指定读出的格式呢,谢谢,
lonki你好,我在code里加入了
use encoding 'UTF-8',FOUT =>'UTF-8';
因为我的输入是一个excel文件,用如下的代码访问
my $Book = $Excel->Workbooks->Open($filein);
    open(FOUT, '>', $fileout) or die "Cannot open $fileout: $!\n";

    my $GlobalSheet = $Book->Worksheets(1);
    my $YahooSheet = $Book->Worksheets(2);
    my $MsnSheet = $Book->Worksheets(3);
    my $OCSheet = $Book->Worksheets(4);
    my $MenuResSheet = $Book->Worksheets(5);

我发现生成的输出文本文件中文部分还是小方格,而且perl执行的过程中打印出
Wide character in print at xxx.pl line xxx
那行就是我输出文件的语句
print FOUT "$value\n";
而且是打印中文的时候,请问是什么问题,谢谢了
Encode的模块一般不需要安装的,Perl自带这个模块
但是CPAN上Encode模块比Unix下的Perl自带的Encode模块多一个pm,Win32下好像是一样的