I need to convert all kinds of files to ANSI (8859-1) except ANSI (8859-1) files, skip those.

When I change my code and try to convert files into utf8 it doesn't work anymore. Seems like the decode line only works like this using decode on utf8. It doesn't distinguish ANSI on ISO.
Or am I doing something wrong?

utf8 to ansi

#!/usr/bin/perl -w use strict; use warnings; use Encode qw(encode decode); my $sFile = ""; my $sLine = ""; my $sCodepoints = ""; if ( $#ARGV == 0 ) { $sFile = $ARGV[0] ; if ( ! -e $sFile ) { die "File '$sFile' doesn't exist!"; } } open( FILE, "<", "$sFile") or die "Couldn't open file '$sFile'!"; { # slurp file into string local $/; $sLine = <FILE>; close( FILE ); } eval { $sCodepoints = decode( "utf8", $sLine, Encode::FB_CROAK ) }; if ( $@ ) { # input was not utf8 print "> No UTF-8, maybe ISO-8859-1 ?\n"; $sCodepoints = $sLine; } open( FILENEW, ">:encoding(iso-8859-1)", "$sFile.new" ) or die "Couldn +'t open file '$sFile.new'!"; print FILENEW $sCodepoints; close( FILENEW );


ansi to utf8

#!/usr/bin/perl -w use strict; use warnings; use Encode qw(encode decode); my $sFile = ""; my $sLine = ""; my $sCodepoints = ""; if ( $#ARGV == 0 ) { $sFile = $ARGV[0] ; if ( ! -e $sFile ) { die "File '$sFile' doesn't exist!"; } } open( FILE, "<", "$sFile") or die "Couldn't open file '$sFile'!"; { # slurp file into string local $/; $sLine = <FILE>; close( FILE ); } 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; } open( FILENEW, ">:encoding(utf8)", "$sFile.new" ) or die "Couldn't ope +n file '$sFile.new'!"; print FILENEW $sCodepoints; close( FILENEW );


I created two test-files ansi.txt and utf8.txt for testing. Content of the files:

1TestöäüÖÄÜß 2TestöäüÖÄÜß 3TestöäüÖÄÜß

In reply to convert files to ansi (8859-1) by Yaerox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.