in reply to convert files to ansi (8859-1)

At least in your second example, you are not properly decoding your input.

eval { $sCodepoints = decode( "iso-8859-1", $sLine, Encode::FB_CROAK ) + }; if ( $@ ) { # input was not iso-8859-1 print "> No ISO-8859-1, maybe UTF8 ?\n"; $sCodepoints = $sLine; }

Here, $sCodepoints does not contain properly decoded UTF-8.

I would try a loop over the possible encodings:

for my $encoding_candidate (qw(iso-8859-1 UTF-8)) { eval { $sCodepoints = decode( $encoding_candidate, $sLine, Encode::FB_ +CROAK ) }; if ( $@ ) { # input was not $encoding_candidate print "> Not $encoding_candidate\n"; #$sCodepoints = $sLine; } }

Replies are listed 'Best First'.
Re^2: convert files to ansi (8859-1)
by Yaerox (Scribe) on Mar 29, 2017 at 08:15 UTC
    At least in your second example, you are not properly decoding your input.
    I don't see what should be wrong? I did the same like in my first example, just turned the utf8 into iso-8859-1 on decode and file-open.

    Your idea using the loop would be my second step. Just now I'm worried because ANSI to UTF8 doesn't work properly.

      You cannot read octets from a file and then hope that Perl will know that you meant UTF-8. You always have to decode your input and encode your output.

        Maybe we talk past each other?

        My utf8 to iso-8859-1 should do the following:
        - read file into scalar
        - eval decode utf8
        -- if we get an error, then i simply say it's iso-8859-1 and I don't do anything
        -- it we get no error, then i use the decoded string

        On the other script iso-8859-1 to utf8, I wanna go the same way:
        - read file into scalar
        - eval decode iso-8859-1
        -- if we get an error, then i simply say it's utf8 and I don't do anything
        -- it we get no error, then i use the decoded string

        Now I expect we get an eval decode iso-8859-1 error if the input file is utf8, but I don't.