简体   繁体   中英

Convert UTF-8 Unix LF .dat file to UTF-8 encoded CR LF .dat file using Perl

I have an input.dat file with Unix LF encoding, I want my output file to be Windows CR LF with UTF-8 encoding using Perl. Currently this is how my code looks. How can I add CR to the existing LF in the file?

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);  
}

Specify the :crlf layer in binmode

binmode DES, ':crlf';

print can't take only the handle, it also needs to know what to print:

 print DES $_;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM