使用Perl

问题描述

我有一个使用Unix LF编码的.dat输入文件,我希望我的输出文件是使用Perl以UTF-8编码的Windows CR LF。目前,这就是我的代码的外观。如何将CR添加文件中的现有LF?

sub encodeUTF8 {
    my $ProcVars = $_[0];
    my $src = $_[1];
    my $des = $_[2];
    # open source file for reading
    open(SRC,'<',$src) or die $!;
    # open destination file for writing
    open(DES,'>',$des) or die $!;
    binmode DES;
    print("copying content from $src to $des\n");
    while (<SRC>) {
        s/^\N{BOM}// if $. == 1;
        print DES;
    }
    close(SRC);
    close(DES);  
}

解决方法

binmode中指定:crlf

binmode DES,':crlf';

print不能只握住手柄,还需要知道要打印什么:

 print DES $_;