Here is a simple program (it puts a value to the database and then gets the value back):
use Encode qw/decode is_utf8/; use DBI; my $dbh = DBI->connect("dbi:Pg:dbname=ab", "***", "***", { RaiseError +=> 1, AutoCommit => 0 }); my $a = decode('iso8859-1', "\x{92}"); $dbh->do("CREATE TABLE a (a text)"); $dbh->do("INSERT INTO a(a) VALUES (?)",{}, $a); my($b) = $dbh->selectrow_array("SELECT * FROM a"); if($b eq $a){ print "Equals a: $a, b: $b\n"; }else{ print "Not equals a: $a, b: $b\n"; } print "a is_utf8: " , is_utf8($a), "\n"; print "b is_utf8: " , is_utf8($b), "\n"; $dbh->rollback; $dbh->disconnect;
And here it's output:
Not equals a: ’, b: Â’ a is_utf8: 1 b is_utf8:
Suprise! The value that we get back is not equal to what we fed to the database. The database is PostgreSQL with UTF8 as the main charset. I am not sure if I did something wrong, or what should be the behaviour of this code snippet, but this struck me as very suprising.

Update: Adding _utf8_on($b) sets the UTF8 flag on $b and then indeed $a eq $b.

Update: The perl version is 5.8.3 on Linux (thanks mirod).

Update: After some more meditation on this fact I see that it is not much different from the case when you feed lets say string '5.0' into a numeric field and when you get it back you have '5'. You just need to remember that the UTF8 flag is lost when the value goes into the database.


In reply to Beware of the UTF_8 flag by zby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.