Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:
Ingredients:
- Perl v5.10.1 (*) built for darwin-2level
- DBI v 1.622
- MySQL Server v 5.1.42
- OS X Terminal App
- GNU bash, version 4.0.35(2)-release (i386-apple-darwin10.8.0)
- BBEdit 9.6.3
MySQL table:
CREATE TABLE `quotes` ( `text` varchar(255) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8
Insert one row with some smart quote characters in it
INSERT INTO `test`.`quotes` ( `text` ) VALUES ( '“” ‘’' );
Command-line MySQL to check the characters got inserted correctly (having connected with --default-character-set=utf8 flag):
mysql> select * from quotes where 1; +----------------+ | text | +----------------+ | “” ‘’ | +----------------+ mysql> select hex(text) from quotes where 1; +----------------------------------+ | hex(text) | +----------------------------------+ | E2809C E2809D 2020 E28098 E28099 | +----------------------------------+ ## just so we know exactly what bytes are in there, my spacing
Minimal perl script to get that row:
#!/usr/local/bin/perl use utf8; use strict; use DBI; my $dbh = DBI->connect( 'DBI:mysql:test:localhost', 'root', 'xxxxxxxx' ) or die("couldn't connect to database"); my $text_display = $dbh->prepare('select * from quotes where 1'); $text_display->execute(); my ($text) = $text_display->fetchrow_array(); print "Text: $text" . $/;
I have my OS X Terminal set to UTF-8 encoding. I have a UTF-8 aware version of bash (I can create files with those quotes in the name and successfully ls and display them.
But when I run the script above from the terminal, it outputs this:
Text: ?? ??
And when I run it via BBEdit's very handy Run command for Unix scripts, I get this:
Text: ìî ëí
Any assistance you can give me will be very gratefully received. Something is obviously going wrong between the database and the output hitting the screen, but everything in the chain is supposed to be UTF-8 aware.
What should I try?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: I'm having a lot of trouble getting UTF-8 output via Perl/DBI on OS X
by remiah (Hermit) on Sep 22, 2012 at 06:48 UTC | |
by Cody Fendant (Hermit) on Sep 22, 2012 at 08:07 UTC | |
by roboticus (Chancellor) on Sep 22, 2012 at 12:26 UTC | |
by remiah (Hermit) on Sep 23, 2012 at 03:06 UTC | |
by Cody Fendant (Hermit) on Sep 22, 2012 at 08:04 UTC |