1package DBM_Filter::encode ;
2
3use strict;
4use warnings;
5use Carp;
6
7our $VERSION = '0.03';
8
9BEGIN
10{
11    eval { require Encode; };
12
13    croak "Encode module not found.\n"
14        if $@;
15}
16
17
18sub Filter
19{
20    my $encoding_name = shift || "utf8";
21
22    my $encoding = Encode::find_encoding($encoding_name) ;
23
24    croak "Encoding '$encoding_name' is not available"
25        unless $encoding;
26
27    return {
28        Store   => sub {
29			 $_ = $encoding->encode($_)
30			     if defined $_ ;
31		   },
32        Fetch   => sub {
33			 $_ = $encoding->decode($_)
34			     if defined $_ ;
35			}
36        } ;
37}
38
391;
40
41__END__
42
43=head1 NAME
44
45DBM_Filter::encode - filter for DBM_Filter
46
47=head1 SYNOPSIS
48
49    use SDBM_File; # or DB_File, GDBM_File, NDBM_File, ODBM_File
50    use DBM_Filter ;
51
52    $db = tie %hash, ...
53    $db->Filter_Push('encode' => 'iso-8859-16');
54
55=head1 DESCRIPTION
56
57This DBM filter allows you to choose the character encoding will be
58store in the DBM file. The usage is
59
60    $db->Filter_Push('encode' => ENCODING);
61
62where "ENCODING" must be a valid encoding name that the Encode module
63recognises.
64
65A fatal error will be thrown if:
66
67=over 5
68
69=item 1
70
71The Encode module is not available.
72
73=item 2
74
75The encoding requested is not supported by the Encode module.
76
77=back
78
79=head1 SEE ALSO
80
81L<DBM_Filter>, L<perldbmfilter>, L<Encode>
82
83=head1 AUTHOR
84
85Paul Marquess pmqs@cpan.org
86
87