This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Adjust VMS test count
[perl5.git] / lib / DBM_Filter / encode.pm
1 package DBM_Filter::encode ;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 our $VERSION = '0.01';
8
9 BEGIN
10 {
11     eval { require Encode; };
12
13     croak "Encode module not found.\n"
14         if $@;
15 }
16
17
18 sub 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
39 1;
40
41 __END__
42
43 =head1 DBM_Filter::encode
44
45 =head1 SYNOPSIS
46
47     use SDBM_File; # or DB_File, or GDBM_File, or NDBM_File, or ODBM_File
48     use DBM_Filter ;
49
50     $db = tie %hash, ...
51     $db->Filter_Push('encode' => 'iso-8859-16');
52     
53 =head1 DESCRIPTION
54
55 This DBM filter allows you to choose the character encoding will be
56 store in the DBM file. The usage is
57
58     $db->Filter_Push('encode' => ENCODING);
59
60 where "ENCODING" must be a valid encoding name that the Encode module
61 recognises.
62
63 A fatal error will be thrown if:
64
65 =over 5
66
67 =item 1
68
69 The Encode module is not available.
70
71 =item 2
72
73 The encoding requested is not supported by the Encode module.
74
75 =back
76
77 =head1 SEE ALSO
78
79 L<DBM_Filter>, L<perldbmfilter>, L<Encode>
80
81 =head1 AUTHOR
82
83 Paul Marquess pmqs@cpan.org
84