This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
warnings: fix POD typo "you" → "your"
[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.03';
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 NAME
44
45 DBM_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
57 This DBM filter allows you to choose the character encoding will be
58 store in the DBM file. The usage is
59
60     $db->Filter_Push('encode' => ENCODING);
61
62 where "ENCODING" must be a valid encoding name that the Encode module
63 recognises.
64
65 A fatal error will be thrown if:
66
67 =over 5
68
69 =item 1
70
71 The Encode module is not available.
72
73 =item 2
74
75 The encoding requested is not supported by the Encode module.
76
77 =back
78
79 =head1 SEE ALSO
80
81 L<DBM_Filter>, L<perldbmfilter>, L<Encode>
82
83 =head1 AUTHOR
84
85 Paul Marquess pmqs@cpan.org
86