14
When you're using UTF8 data, you may run into the "Wide character in print"
15
warning. To fix that in 5.8+, do the following:
17
my $db = DBM::Deep->new( ... );
18
binmode $db->_fh, ":utf8";
20
In 5.6, you will have to do the following:
22
my $db = DBM::Deep->new( ... );
23
$db->set_filter( 'store_value' => sub { pack "U0C*", unpack "C*", $_[0] } );
24
$db->set_filter( 'retrieve_value' => sub { pack "C*", unpack "U0C*", $_[0] } );
26
In a future version, you will be able to specify C<utf8 =E<gt> 1> and
27
L<DBM::Deep> will do these things for you.
14
If possible, it is highly recommended that you upgrade your database to
15
version 2 (using the F<utils/upgrade_db.pl> script in the CPAN
16
distribution), in order to use Unicode.
18
If your databases are still shared by perl installations with older
19
DBM::Deep versions, you can use filters to encode strings on the fly:
21
my $db = DBM::Deep->new( ... );
22
my $encode_sub = sub { my $s = shift; utf8::encode($s); $s };
23
my $decode_sub = sub { my $s = shift; utf8::decode($s); $s };
24
$db->set_filter( 'store_value' => $encode_sub );
25
$db->set_filter( 'fetch_value' => $decode_sub );
26
$db->set_filter( 'store_key' => $encode_sub );
27
$db->set_filter( 'fetch_key' => $decode_sub );
29
A previous version of this cookbook recommended using
30
C<binmode $db-E<gt>_fh, ":utf8">, but that is I<not> a good idea, as it
31
could easily corrupt the database.
29
33
=head2 Real-time Encryption Example
31
35
B<NOTE>: This is just an example of how to write a filter. This most
32
36
definitely should B<NOT> be taken as a proper way to write a filter that does
37
encryption. (Furthermore, it fails to take Unicode into account.)
35
39
Here is a working example that uses the I<Crypt::Blowfish> module to
36
40
do real-time encryption / decryption of keys & values with DBM::Deep Filters.
102
106
sub my_compress {
103
return Compress::Zlib::memGzip( $_[0] ) ;
109
return Compress::Zlib::memGzip( $s ) ;
105
111
sub my_decompress {
106
return Compress::Zlib::memGunzip( $_[0] ) ;
112
my $s = Compress::Zlib::memGunzip( shift ) ;
109
117
B<Note:> Filtering of keys only applies to hashes. Array "keys" are