skip@pobox.com has asked for the wisdom of the Perl Monks concerning the following question:

Based on what I saw in another q/a about Unicode on this site I tried: perl -MUnicode::Map8 -e'print Unicode::Map8->new("utf8")->to8(<>)' to convert a UTF-8 string (output from XML::Parser) to Latin-1 (for insertion into an HTML document). When I pasted in Caffè Lena (the two characters after "Caff" represent an "e" with a grave accent) it complained Can't call method "to8" on an undefined value at -e line 1, <> line 1. I'm a mediocre (at best) Perl programmer. Adding Unicode to the mix really isn't helping my blood pressure. This has to be something really trivial. Any help much appreciated. Thx, Skip

Replies are listed 'Best First'.
Re: How to get latin-1 out of a utf-8 string (5.6.1)?
by mirod (Canon) on Dec 29, 2001 at 05:13 UTC

    Try using Text::Iconv instead of Unicode::map8. Actually if iconv is installed on your machine you can even use it separately: iconv -f UTF8 -t LATIN1 <file>

    use Text::Iconv; $converter = Text::Iconv->new("UTF8", "LATIN1"); $converted = $converter->convert( $utf8_string);
Re: How to get latin-1 out of a utf-8 string (5.6.1)?
by moodster (Hermit) on Dec 29, 2001 at 20:35 UTC
    Or, since there is more than one way to do it, try this:
    use Unicode::String qw( latin1 utf8 ); while ( <> ) { print utf8( $_ )->latin1; }
    Unicode::String doesn't seem to be part of the standard distribution though. Does anyone know why? It seems important and useful enough...

    Cheers,
    -- moodster