Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

LATIN1 and utf8 strings, joined, mangle the LATIN1

by brycen (Monk)
on Feb 05, 2009 at 23:35 UTC ( [id://741738]=perlquestion: print w/replies, xml ) Need Help??

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

So I just learned that if you have a LATIN1 string, and a UTF-8 string, and you join them, the result is utf8, and the LATIN1 is mangled:
#! /usr/bin/perl # Perl v5.8.8 use strict; use Data::Dumper; use Encode; my $string1 = "weight_\x{2639}"; # Valid Unicode my $string2 = "weight_\xc2"; # Valid iso-8859-1 my @words; push(@words,$string1); push(@words,$string2); my $str = join(",", @words); print STDERR "mangled : ".print_chrcodes($str)."\n"; my @words; Encode::_utf8_off($string1); push(@words,$string1); Encode::_utf8_off($string2); push(@words,$string2); my $str = join(",", @words); print STDERR "preserved: ".print_chrcodes($str)."\n"; ############################################################# sub print_chrcodes { my $str = shift; my $ret; foreach my $ascval (unpack("C*", $str)) { $ret .= chr($ascval) if($ascval < 128); $ret .= sprintf ("<#%x>",$ascval) if($ascval >= 128); } return $ret; }
# perl -v This is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi # perl test.pl mangled : weight_<#e2><#98><#b9>,weight_<#c3><#82> preserved: weight_<#e2><#98><#b9>,weight_<#c2>

We have a DBI / DBI::Pg application that does "SET CLIENT ENCODING LATIN1", which has been running for years. A one point it comes up with a list of "unique" words, which have always had the rare duplicate. The DB results get split into words, some of which get Perl's magic utf8 flag, some not, triggering the above when joined again. The actual examples are valid sequences of LATIN1 that are also, viewed as bytes, valid utf8.

The question for the Monks is "should I call _utf8_off() on all strings coming from the database?". Is that fast, cheap, and will the utf8_off flag persist once the string is split and combined and processed through many levels of code? Will the application run notably faster on byte oriented strings? And what kind of unintended consequences might I face? See also http://www.perlmonks.org/?node_id=314423

And "is this a reportable bug in join()"?

Replies are listed 'Best First'.
Re: LATIN1 and utf8 strings, joined, mangle the LATIN1
by ikegami (Patriarch) on Feb 05, 2009 at 23:54 UTC

    $str contains "weight_\x{2639},weight_\x{c2}", what I would expect from joining "weight_\x{2639}" and "weight_\x{c2}". All I see missing is where you encod your output

    # Encode input from/output to STDIN/STDOUT/STDERR as per locale use open ':std', ':locale';
    # Force UTF-8 input/output for STDIN/STDOUT/STDERR use open ':std', ':encoding(UTF-8)';

    You'll get better diagnostic output from Devel::Peek's Dump. For example, Dump($str) gives

    SV = PV(0x81e829c) at 0x814ecdc REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8) PV = 0x81fe838 "weight_\342\230\271,weight_\303\202"\0 [UTF8 "weight +_\x{2639},weight_\x{c2}"] CUR = 20 LEN = 24

    You see both the external and internal representation.

      In the use case, the data is not really output. The joined string is inserted back into the database (as noted above, mangling the output). This application was written prior to perl's support for unicode and/or utf-8.
        So it's being output to the database instead of the screen. use open won't help you, but it still needs to be encoded.

        Sorry, I don't think I can help you because I don't know enough about how database handle encodings, what the DBD expects from you and what the DBD can do for you.

Re: LATIN1 and utf8 strings, joined, mangle the LATIN1
by Joost (Canon) on Feb 05, 2009 at 23:49 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://741738]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2024-04-24 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found