I'm going to describe how DBD::mysql works. I suspect DBD::Pg works the same way.
Perl has two ways of storing strings. DBI or DBD::mysql looks at the internal buffer of scalars without checking which storage format was used, so every time you pass a string, it's as if you actually passed
use Encode qw( is_utf8 encode_utf8 );
is_utf8($string) ? encode_utf8($string) : $string
This is a bug, but it almost always does the right thing.
Workaround:
- If you have a decoded string (a string of Unicode code points), you can use the following:
use Encode qw( encode_utf8 );
$dbh->do("SET NAMES utf8");
my $sth = ...;
$sth->execute(encode_utf8($decoded));
- If you have a string encoded using cp1252, you can use the following:
use Encode qw( decode );
$dbh->do("SET NAMES utf8");
my $sth = ...;
$sth->execute(decode('cp1252', $encoded));
- If you have a string encoded using cp1252 you want to avoid any encoding and decoding on the Perl side, you can use the following:
sub _d { my ($s) = @_; utf8::downgrade($_); $s }
$dbh->do("SET NAMES cp1252");
my $sth = ...;
$sth->execute(_d($encoded));
Notes:
- Passing mysql_enable_utf8=>1 to DBI->connect does $dbh->do("SET NAMES utf8"); for you. Later changes to mysql_enable_utf8 does not.
- is_utf8 always returns true for strings returned by Encode::decode and Encode::decode_utf8.
- is_utf8 always returns false for strings returned by Encode::encode, Encode::encode_utf8 and Encode::from_to.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.