in reply to quick translate utf8 to latin1 encoding

cat utf8.txt | perl -C1 -pe1 >latin1.txt

At least, this works for me :)

(All UTF-8 characters to convert must be representable in the Latin1 character set.)

Replies are listed 'Best First'.
Re^2: quick translate utf8 to latin1 encoding
by ikegami (Patriarch) on Feb 16, 2007 at 01:43 UTC

    It works fine for characters in the native charset...

    # feeder.pl $s = join '', map chr, 0..255; print $s;
    # tester.pl $s = join '', map chr, 0..255; 1 while $read = read(STDIN, $in, 512, $ofs+=$read); print($in eq $s ? "pass\n" : "fail\n");
    $ perl feeder.pl | perl tester.pl pass $ perl feeder.pl | perl -C6 -pe1 | perl -C1 -pe1 | perl tester.pl pass

    ... But it doesn't work so well for characters not in the native charset.

    $ perl -C6 -e 'print "\x{2660}"' | perl -C1 -pe1 Wide character in print, <> line 1. [junk][junk][junk]

    Compare to

    $ perl -C6 -e 'print "\x{2660}"' | perl -C1 -MEncode -pe '$_=encode("i +so-latin-1",$_);' ?

    Up to the reader to choose if that's good enough.