This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use a switch in pp_prototype for compactness
[perl5.git] / lib / DBM_Filter / encode.pm
CommitLineData
0e9b1cbd
PM
1package DBM_Filter::encode ;
2
3use strict;
4use warnings;
5use Carp;
6
ae8d64f5 7our $VERSION = '0.02';
0e9b1cbd
PM
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 {
b370a9eb
RGS
28 Store => sub {
29 $_ = $encoding->encode($_)
0e9b1cbd
PM
30 if defined $_ ;
31 },
b370a9eb 32 Fetch => sub {
0e9b1cbd
PM
33 $_ = $encoding->decode($_)
34 if defined $_ ;
35 }
36 } ;
37}
38
391;
40
41__END__
42
b370a9eb
RGS
43=head1 NAME
44
45DBM_Filter::encode - filter for DBM_Filter
0e9b1cbd
PM
46
47=head1 SYNOPSIS
48
49 use SDBM_File; # or DB_File, or GDBM_File, or NDBM_File, or ODBM_File
50 use DBM_Filter ;
51
52 $db = tie %hash, ...
53 $db->Filter_Push('encode' => 'iso-8859-16');
b370a9eb 54
0e9b1cbd
PM
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