I have to read a text file in Perl which is encoded as UTF-8, this is working fine. My output file OUT_2 has to be encoded as ISO-8859-1 (aka "Latin1"). I tried this code (and some more) but my output file OUT_2 is always written as UTF-8. Any idea to achieve it?
use strict;
use Encode::Encoder;

open IN, "c:/Temp/Input.txt"; # this file is UTF-8

open OUT_1, ">", "c:/Temp/out_1.txt";
# encoding of OUT_1 does not matter because it contains only ASCII
open OUT_2, ">:encoding(latin1)", "c:/Temp/out_2.txt";

my $line = 1;
while ( ) {
chomp;
print OUT_1 "Write line $line\n";
print OUT_2 "$_ and some stuff\n";
$line++;
}

close IN;
close OUT_1;
close OUT_2;
This proposal does not work either:
my $data = "$_ and some stuff\n";
Encode::encode("latin1", Encode::decode("UTF-8", $data));
print OUT_2 $data;


Check Solution