Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The problem being that I can't get it to work and there are no error messages.
use Unicode::Map8; my($content) = some UTF8 string being passed in. my($latin_map) = Unicode::Map8->new("latin1") || die("Map8 problem"); my($latin_string) = $latin_map->to8($content); print "Latin string is $latin_string";
I know that the utf8->latin1 conversion is lossy but getting nothing back seems a bit extreme to me. Am I forgetting something obvious?

Replies are listed 'Best First'.
Re: Unicode::Map8 problems
by Anonymous Monk on Jan 31, 2001 at 20:46 UTC
    Ok, I knew this would happen, I'd make a post and then discover a solution. The problem was that map8 was not recognizing $content as an utf8 string (being read in from an external source) and if I used Unicode::String to make an explicit utf8 string and pass it to map, it works as expected.

    Sorry for wasting people's time.

Re: Unicode::Map8 problems
by mirod (Canon) on Jan 31, 2001 at 20:31 UTC

    A first problem is that you use my($var) = scalar expression instead of my $var= scalar_expression try removing all those extra ()

    But the real problem is that I don't think Unicode::Map8 does work the way you think it does. As I am not really familiar with the module I won't give you a solution using it, but I lifted one to convert unicode to latin1 from XML::TiePYX:

    sub utf8_to_latin1 { $_[0]=~s{([\xc0-\xc3])(.)}{ my $hi = ord($1); my $lo = ord($2); chr((($hi & 0x03) <<6) | ($lo & 0x3F)) }ge; }