0
votes

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

1 Answers

3
votes

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 $_;