Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic


by skazat (Chaplain)
on Feb 25, 2011 at 19:26 UTC ( #890211=perlquestion: print w/replies, xml ) Need Help??

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/ 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 +/ (autosplit into blib/lib/auto/Storable/ line 421 +, at ../perllib/MLDBM/Serializer/ line 27

Which makes very very little sense to me at all.

35.114? My brothers/sisters, I'm sincerely lost in the woods, with this one.


Replies are listed 'Best First'.
Re: MLDBM and UTF-8
by ig (Vicar) on Feb 28, 2011 at 07:08 UTC

    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.

    I wrote a small test using Storable and it appears to work.

    use strict; use warnings; use MLDBM qw(DB_File Storable); use Devel::Peek; use Encode; use FreezeThaw; no warnings 'utf8'; $| = 1; tie my %data, 'MLDBM', 'testmldbm' or die $!; my $string = "a string with a wide character: \x{0100}"; Dump($string); #my $fstring = FreezeThaw::freeze($string); $data{string} = $string; print "$data{string}\n";

    It produced the following output, but cut-and-paste seems to have mangled the wide character. It looks like a capital A with a bar above it on my terminal:

    SV = PV(0x9766470) at 0x964eae8 REFCNT = 1 FLAGS = (PADMY,POK,pPOK,UTF8) PV = 0x97a5190 "a string with a wide character: \304\200"\0 [UTF8 "a + string with a wide character: \x{100}"] CUR = 34 LEN = 36 a string with a wide character: Ā

    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:

    use strict; use warnings; use MLDBM qw(DB_File FreezeThaw); use Devel::Peek; use Encode; use FreezeThaw; no warnings 'utf8'; use Data::Dumper; use DBM_Filter; $| = 1; my $db = tie my %data, 'MLDBM', 'testmldbm' or die $!; print Dumper($db->{DB}); $db->{DB}->Filter_Push('utf8'); if($db->{DB}->can('Filter_Push')) { print "can Filter_Push\n"; } my $string = "a string with a wide character: \x{0100}"; Dump($string); $data{string} = $string; print "$data{string}\n";

    With the filter added, this test script does not produce the Wide character error - it seems to work.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://890211]
Approved by Corion
Front-paged by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-01-19 15:12 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (55 votes). Check out past polls.