File: Encode.pm

package info (click to toggle)
mhonarc 2.6.24-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,020 kB
  • sloc: perl: 280,086; makefile: 11
file content (172 lines) | stat: -rw-r--r-- 4,701 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
##---------------------------------------------------------------------------##
##  File:
##	$Id: Encode.pm,v 1.2 2002/12/20 08:01:11 ehood Exp $
##  Author:
##      Earl Hood       earl@earlhood.com
##  Description:
##	POD after __END__.
##---------------------------------------------------------------------------##
##    Copyright (C) 2002	Earl Hood, earl@earlhood.com
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation; either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
##    02111-1307, USA
##---------------------------------------------------------------------------##

package MHonArc::Encode;

use strict;

our $VERSION = '2.6.24';

BEGIN {
    # If the Encode module is available, we use it, otherwise, we
    # try to use Unicode::MapUTF8.
    eval { require Encode; };
    if (!$@) {
        *from_to = \&_encode_from_to;
    } else {
        require Unicode::MapUTF8;
        *from_to = \&_unimap_from_to;
    }
}

##---------------------------------------------------------------------------##

sub _encode_from_to {
    my $text_r   = shift;
    my $from_enc = lc shift;
    my $to_enc   = lc shift;

    return '' if $$text_r eq '';

    # Strip utf8 string flag if set
    if (Encode::is_utf8($$text_r)) {
        $$text_r = Encode::encode('utf8', $$text_r);
    }
    my $is_error = 0;
    eval {
        if (!defined(Encode::from_to($$text_r, $from_enc, $to_enc))) {
            warn qq/Warning: MHonArc::Encode: Unable to convert /,
                qq/"$from_enc" to "$to_enc"\n/;
            $is_error = 1;
        }
    };
    if ($@) {
        warn qq/Warning: $@\n/;
        $is_error = 1;
    }
    $is_error ? undef : $to_enc;
}

sub _unimap_from_to {
    my $text_r   = shift;
    my $from_enc = lc shift;
    my $to_enc   = lc shift;

    if (!Unicode::MapUTF8::utf8_supported_charset($from_enc)) {
        warn qq/Warning: MHonArc::Encode "$from_enc" not supported\n/;
        return undef;
    }
    if (!Unicode::MapUTF8::utf8_supported_charset($to_enc)) {
        warn qq/Warning: MHonArc::Encode "$to_enc" not supported\n/;
        return undef;
    }
    $$text_r = Unicode::MapUTF8::to_utf8(
        {-string => $$text_r, -charset => $from_enc});
    $$text_r = Unicode::MapUTF8::from_utf8(
        {-string => $$text_r, -charset => $to_enc});
    $to_enc;
}

##---------------------------------------------------------------------------##
1;
__END__

=head1 NAME

MHonArc::Encode - Text encoding routines for MHonArc

=head1 SYNOPSIS

  <TextEncode>
  charset; MHonArc::Encode::from_to; MHonArc/Encode.pm
  </TextEncode>

=head1 DESCRIPTION

MHonArc::Encode provides support for converting text in one
encoding to text in another encoding.

If you converting all data into utf-8, it is recommended
to use the L<MHonArc::UTF8|MHonArc::UTF8> module instead.

=head1 FUNCTIONS

=over

=item C<MHonArc::Encode::from_to($data_ref, $from_charset, $to_charset)>

This function is designed to be registered to the TEXTENCODE
resource:

  <TextEncode>
  charset; MHonArc::Encode::from_to; MHonArc/Encode.pm
  </TextEncode>

Converts C<$data_ref> encoded in C<$from_charset> into C<$to_charset>).
C<$data_ref> should be a reference to a scalar string.  Conversion is
done in-place.

C<undef> is returned if conversion from C<$from_charset> to
C<$to_charset>) is not supported.

=back

=head1 NOTES

=over

=item *

If available, the L<Encode|Encode> module is used for converting
the text.  If not available,
the L<Unicode::MapUTF8|Unicode::MapUTF8> module is used.

The Encode module is only provided with Perl 5.8, and later.
The Unicode::MapUTF8 module is available via CPAN, but require Perl
5.6, or later.

=back

=head1 SEE ALSO

L<MHonArc::UTF8|MHonArc::UTF8>

The TEXTENCODE and CHARSETCONVERTERS resources in the MHonArc documentation.

=head1 VERSION

C<$Id: Encode.pm,v 1.2 2002/12/20 08:01:11 ehood Exp $>

=head1 AUTHOR

Earl Hood, earl@earlhood.com

MHonArc comes with ABSOLUTELY NO WARRANTY and MHonArc may be copied only
under the terms of the GNU General Public License, which may be found in
the MHonArc distribution.

=cut