MySQL Forums
Forum List  »  Perl

utf8 and perl Storable - solution
Posted by: John ORourke
Date: August 01, 2007 06:03AM

I recently moved my database and application to use UTF8 end-to-end, and I had the same problem.

I'm use Storable freeze and thaw but of course freeze outputs an arbitrary string of octets. When that arbitrary string (which might or might not be valid utf8) goes into the utf8 database field, then comes back out, Perl treats it as utf8. Feed that into thaw() and you're not likely to get a valid object back.

The solution is easy:

-------------------------
sub freeze_to_utf8 {
my $v=freeze(shift);
utf8::upgrade($v) or undef $v; # should never fail
return $v;
}
-------------------------
sub thaw_from_utf8 {
my $v=shift;
if(utf8::downgrade($v)){
return thaw($v);
}else{
return undef;
}
}
-------------------------

hope this helps, let me know!

John - www.versatilia.com

Options: ReplyQuote


Subject
Written By
Posted
utf8 and perl Storable - solution
August 01, 2007 06:03AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.