Yaerox has asked for the wisdom of the Perl Monks concerning the following question:
#!/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 );
#!/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 );
1TestöäüÖÄÜß 2TestöäüÖÄÜß 3TestöäüÖÄÜß
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: convert files to ansi (8859-1)
by Corion (Patriarch) on Mar 29, 2017 at 07:54 UTC | |
by Yaerox (Scribe) on Mar 29, 2017 at 08:15 UTC | |
by Corion (Patriarch) on Mar 29, 2017 at 08:17 UTC | |
by Yaerox (Scribe) on Mar 29, 2017 at 08:33 UTC | |
by Corion (Patriarch) on Mar 29, 2017 at 08:35 UTC | |
| |
Re: convert files to ansi (8859-1)
by vrk (Chaplain) on Mar 29, 2017 at 08:39 UTC | |
by Yaerox (Scribe) on Mar 29, 2017 at 08:40 UTC | |
Re: convert files to ansi (8859-1)
by Anonymous Monk on Mar 29, 2017 at 08:02 UTC | |
by AppleFritter (Vicar) on Mar 29, 2017 at 09:16 UTC | |
by marto (Cardinal) on Mar 29, 2017 at 09:20 UTC | |
by AppleFritter (Vicar) on Mar 29, 2017 at 09:26 UTC | |
by marto (Cardinal) on Mar 29, 2017 at 09:30 UTC | |
by beech (Parson) on Mar 29, 2017 at 10:30 UTC | |
| |
by Yaerox (Scribe) on Mar 29, 2017 at 08:20 UTC |