skazat has asked for the
wisdom of the Perl Monks concerning the following question:

Hello everyone,

I'm having trouble saving UTF-8 text using MLDB.

I understand that the value needs to be encoded on save and decoded before being used, but MLDB provides a additional problem, since you're not just passing a string to it, but instead a complex data structure - you cannot attempt to encode the complex data structure and think that's going to do the trick - it doesn't even make sense.

This means you probably need to encode/decode (at the right time) each value separately.

Is there any easy way to do this? It seems that you would have to walk through the entire data structure itself. Sadly, I don't know a bulletproof way to do that, in this instance. I do know the format of what to expect of this data structure, but it may change in time. Mostly, it's just a hashref of hashrefs, perhaps with some arrayrefs - no objects, or strange things like that.

Has anyone already stumbled on this problem?

Attempting to save a value with an unencoded UTF-8 string in one of values in a complex data structure using MLDBM will give back this error:

Wide character in subroutine entry at ../perllib/MLDBM.pm line 161.

I'm using MLDBM with the FreezeThaw backend:

use MLDBM qw(DB_File FreezeThaw);

as using Storable:
use MLDBM qw(DB_File Storable);

produces this delightful error:

Storable binary image v35.114 more recent than I am (v2.8) at blib/lib+/Storable.pm (autosplit into blib/lib/auto/Storable/thaw.al) line 421+, at ../perllib/MLDBM/Serializer/Storable.pm line 27

Regarding Storable, the error message is indicating that the binary data it is trying to retrieve was written by Storable version 35.114. Since current version is 2.8, it seems most likely that you are trying to read corrupt data.

Changing the script to use FreezeThaw, it reproduces your error. This appears to be a limitation of DB_File. Apparently FreezeThaw passes the wide characters through to DB_File while Storable does not.

Maybe you can use FreezeThaw with a filter, something like the following:

When putting a smiley right before a closing parenthesis, do you:

Use two parentheses: (Like this: :) )
Use one parenthesis: (Like this: :)
Reverse direction of the smiley: (Like this: (: )
Use angle/square brackets instead of parentheses
Use C-style commenting to set the smiley off from the closing parenthesis
Make the smiley a dunce: (:>
I disapprove of emoticons
Other