After having spent the better part of a day figuring out how perfectly good UTF-8 data out of a MySQL database was being mangled by Perl, I found that this was caused by Perl upgrading the string which was already UTF-8 (but didn't have the internal UTF-8 flag set). So I needed a way to set that flag, I found the "_utf8_on" of Encode through graff's answer Re: restore unicode data from database?.
However, doing this "manually" on everything you fetch out of a database, becomes tedious quickly. Since I frequently use the selectall_arrayref fetching methods, I created this little sub that could can "wrap" around such a call.
It expects the arrayref of arrayrefs as input, and also returns it. So you can "inline" the call to "_utf8_on_all_arrayref".
require Encode; # needs to be done only once in the beginning sub _utf8_on_all_arrayref { # For all the records specified # Switch on the UTF-8 flag for all values # Return the original reference foreach (@{$_[0]}) { Encode::_utf8_on( $_ ) foreach @{$_}; } $_[0]; } #_utf8_on_all_arrayref
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Switching on internal UTF-8 flaq on DBI result from database
by Aristotle (Chancellor) on Dec 14, 2003 at 17:19 UTC | |
by liz (Monsignor) on Dec 14, 2003 at 20:46 UTC | |
by Aristotle (Chancellor) on Dec 14, 2003 at 23:15 UTC |
Back to
Cool Uses for Perl