use strict; use DBI; use Devel::Peek; use bytes; no bytes; my ($txt_de, $txt_ru); { use utf8; $txt_de = 'Käse'; $txt_ru = 'Москва'; } binmode STDOUT, ':utf8'; my @dsn = qw/DBI:ODBC:xxx xx xx/; my %opt = (PrintError => 0, RaiseError => 1, AutoCommit => 1, ChopBlanks => 1); my $dbh = DBI->connect( @dsn, \%opt ); $dbh->{LongReadLen} = 4000; $dbh->{LongTruncOk} = 1; my $sth_ins = $dbh->prepare( 'INSERT INTO T2 (a, u, x) VALUES (?, ?, CAST( ? AS XML) )' ); foreach my $row ([$txt_de, $txt_de, "$txt_de"], [$txt_ru, $txt_ru, "$txt_ru"]) { $sth_ins->bind_param(1, $row->[0]); $sth_ins->bind_param(2, $row->[1]); $sth_ins->bind_param(3, $row->[2], {TYPE => -8}); $sth_ins->execute; } #$sth_ins->execute( $txt_de, $txt_de, "$txt_de" ); #$sth_ins->execute( $txt_ru, $txt_ru, "$txt_ru" ); my $sth_sel = $dbh->prepare( 'SELECT u, x FROM T2' ); $sth_sel->execute; $sth_sel->bind_col(1, \my $txt, {TYPE => -8}); $sth_sel->bind_col(2, \my $xml, {TYPE => -8}); #$sth_sel->bind_columns( \my( $txt, $xml ) ); my $i = 0; while ( $sth_sel->fetch ) { printf "%3u %3u %3u %s [%s] [%s]\n", ++$i, length($txt), bytes::length($txt), (utf8::is_utf8($txt) ? ' utf8' : '!utf8'), $txt, $xml; # NOTE, if I don't reset $txt each iteration the length() call returns # the wrong answer. #$txt = ''; this line fixes it # http://code.activestate.com/lists/perl5-porters/153703/ Dump($txt); } $dbh->disconnect; #### perl -Iblib/lib -Iblib/arch examples/xml2.pl 1 4 5 utf8 [Käse] [Käse] # ^ this 4 and 5 are correct SV = PVMG(0x853b6fc) at 0x856d570 REFCNT = 2 FLAGS = (PADMY,SMG,POK,pPOK,UTF8) IV = 0 NV = 0 PV = 0x8674698 "K\303\244se"\0 [UTF8 "K\x{e4}se"] CUR = 5 LEN = 8 MAGIC = 0x86746c0 MG_VIRTUAL = &PL_vtbl_utf8 MG_TYPE = PERL_MAGIC_utf8(w) MG_LEN = 4 2 4 12 utf8 [Москва] [Москва] # ^ this 4 is wrong but the 12 is probably right SV = PVMG(0x853b6fc) at 0x856d570 REFCNT = 2 FLAGS = (PADMY,SMG,POK,pPOK,UTF8) IV = 0 NV = 0 PV = 0x8671098 "\320\234\320\276\321\201\320\272\320\262\320\260"\0 [UTF8 "\x{41c}\x{43e}\x{441}\x{43a}\x{432}\x{430}"] CUR = 12 LEN = 16 MAGIC = 0x86746c0 MG_VIRTUAL = &PL_vtbl_utf8 MG_TYPE = PERL_MAGIC_utf8(w) MG_LEN = 4