Commit | Line | Data |
18586f54 |
1 | package Encode::Encoding; |
d1256cb1 |
2 | |
18586f54 |
3 | # Base class for classes which implement encodings |
4 | use strict; |
656ebd29 |
5 | use warnings; |
742555bd |
6 | our $VERSION = do { my @r = ( q$Revision: 2.5 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r }; |
621b0f8d |
7 | |
8 | require Encode; |
18586f54 |
9 | |
cc836e95 |
10 | sub DEBUG { 0 } |
d1256cb1 |
11 | |
12 | sub Define { |
13 | my $obj = shift; |
18586f54 |
14 | my $canonical = shift; |
d1256cb1 |
15 | $obj = bless { Name => $canonical }, $obj unless ref $obj; |
16 | |
18586f54 |
17 | # warn "$canonical => $obj\n"; |
d1256cb1 |
18 | Encode::define_encoding( $obj, $canonical, @_ ); |
18586f54 |
19 | } |
20 | |
d1256cb1 |
21 | sub name { return shift->{'Name'} } |
a0d8a30e |
22 | |
742555bd |
23 | sub mime_name{ |
24 | require Encode::MIME::Name; |
25 | return Encode::MIME::Name::get_mime_name(shift->name); |
26 | } |
27 | |
cc836e95 |
28 | # sub renew { return $_[0] } |
29 | |
30 | sub renew { |
31 | my $self = shift; |
d1256cb1 |
32 | my $clone = bless {%$self} => ref($self); |
33 | $clone->{renewed}++; # so the caller can see it |
cc836e95 |
34 | DEBUG and warn $clone->{renewed}; |
35 | return $clone; |
36 | } |
37 | |
d1256cb1 |
38 | sub renewed { return $_[0]->{renewed} || 0 } |
cc836e95 |
39 | |
a0d8a30e |
40 | *new_sequence = \&renew; |
10c5ecbb |
41 | |
d1256cb1 |
42 | sub needs_lines { 0 } |
10c5ecbb |
43 | |
d1256cb1 |
44 | sub perlio_ok { |
45 | eval { require PerlIO::encoding }; |
10c5ecbb |
46 | return $@ ? 0 : 1; |
47 | } |
18586f54 |
48 | |
a0d8a30e |
49 | # (Temporary|legacy) methods |
50 | |
d1256cb1 |
51 | sub toUnicode { shift->decode(@_) } |
52 | sub fromUnicode { shift->encode(@_) } |
18586f54 |
53 | |
10c5ecbb |
54 | # |
55 | # Needs to be overloaded or just croak |
56 | # |
18586f54 |
57 | |
10c5ecbb |
58 | sub encode { |
59 | require Carp; |
60 | my $obj = shift; |
61 | my $class = ref($obj) ? ref($obj) : $obj; |
d1256cb1 |
62 | Carp::croak( $class . "->encode() not defined!" ); |
10c5ecbb |
63 | } |
0ab8f81e |
64 | |
d1256cb1 |
65 | sub decode { |
10c5ecbb |
66 | require Carp; |
67 | my $obj = shift; |
68 | my $class = ref($obj) ? ref($obj) : $obj; |
d1256cb1 |
69 | Carp::croak( $class . "->encode() not defined!" ); |
10c5ecbb |
70 | } |
6d1c0808 |
71 | |
d1256cb1 |
72 | sub DESTROY { } |
284ee456 |
73 | |
18586f54 |
74 | 1; |
75 | __END__ |
1b2c56c8 |
76 | |
77 | =head1 NAME |
78 | |
79 | Encode::Encoding - Encode Implementation Base Class |
80 | |
81 | =head1 SYNOPSIS |
82 | |
83 | package Encode::MyEncoding; |
84 | use base qw(Encode::Encoding); |
85 | |
86 | __PACKAGE__->Define(qw(myCanonical myAlias)); |
87 | |
5129552c |
88 | =head1 DESCRIPTION |
1b2c56c8 |
89 | |
90 | As mentioned in L<Encode>, encodings are (in the current |
10c5ecbb |
91 | implementation at least) defined as objects. The mapping of encoding |
92 | name to object is via the C<%Encode::Encoding> hash. Though you can |
93 | directly manipulate this hash, it is strongly encouraged to use this |
94 | base class module and add encode() and decode() methods. |
1b2c56c8 |
95 | |
10c5ecbb |
96 | =head2 Methods you should implement |
1b2c56c8 |
97 | |
10c5ecbb |
98 | You are strongly encouraged to implement methods below, at least |
99 | either encode() or decode(). |
1b2c56c8 |
100 | |
101 | =over 4 |
102 | |
10c5ecbb |
103 | =item -E<gt>encode($string [,$check]) |
1b2c56c8 |
104 | |
0ab8f81e |
105 | MUST return the octet sequence representing I<$string>. |
106 | |
107 | =over 2 |
108 | |
109 | =item * |
110 | |
111 | If I<$check> is true, it SHOULD modify I<$string> in place to remove |
112 | the converted part (i.e. the whole string unless there is an error). |
113 | If perlio_ok() is true, SHOULD becomes MUST. |
114 | |
115 | =item * |
116 | |
117 | If an error occurs, it SHOULD return the octet sequence for the |
118 | fragment of string that has been converted and modify $string in-place |
119 | to remove the converted part leaving it starting with the problem |
120 | fragment. If perlio_ok() is true, SHOULD becomes MUST. |
121 | |
122 | =item * |
1b2c56c8 |
123 | |
0ab8f81e |
124 | If I<$check> is is false then C<encode> MUST make a "best effort" to |
125 | convert the string - for example, by using a replacement character. |
126 | |
127 | =back |
1b2c56c8 |
128 | |
10c5ecbb |
129 | =item -E<gt>decode($octets [,$check]) |
1b2c56c8 |
130 | |
0ab8f81e |
131 | MUST return the string that I<$octets> represents. |
132 | |
133 | =over 2 |
134 | |
135 | =item * |
136 | |
137 | If I<$check> is true, it SHOULD modify I<$octets> in place to remove |
138 | the converted part (i.e. the whole sequence unless there is an |
139 | error). If perlio_ok() is true, SHOULD becomes MUST. |
140 | |
141 | =item * |
1b2c56c8 |
142 | |
0ab8f81e |
143 | If an error occurs, it SHOULD return the fragment of string that has |
144 | been converted and modify $octets in-place to remove the converted |
145 | part leaving it starting with the problem fragment. If perlio_ok() is |
146 | true, SHOULD becomes MUST. |
147 | |
148 | =item * |
149 | |
150 | If I<$check> is false then C<decode> should make a "best effort" to |
1b2c56c8 |
151 | convert the string - for example by using Unicode's "\x{FFFD}" as a |
152 | replacement character. |
153 | |
154 | =back |
155 | |
8676e7d3 |
156 | =back |
157 | |
158 | If you want your encoding to work with L<encoding> pragma, you should |
159 | also implement the method below. |
160 | |
161 | =over 4 |
162 | |
220e2d4e |
163 | =item -E<gt>cat_decode($destination, $octets, $offset, $terminator [,$check]) |
164 | |
165 | MUST decode I<$octets> with I<$offset> and concatenate it to I<$destination>. |
166 | Decoding will terminate when $terminator (a string) appears in output. |
167 | I<$offset> will be modified to the last $octets position at end of decode. |
168 | Returns true if $terminator appears output, else returns false. |
169 | |
151b5d36 |
170 | =back |
171 | |
10c5ecbb |
172 | =head2 Other methods defined in Encode::Encodings |
173 | |
174 | You do not have to override methods shown below unless you have to. |
175 | |
176 | =over 4 |
177 | |
178 | =item -E<gt>name |
179 | |
180 | Predefined As: |
181 | |
182 | sub name { return shift->{'Name'} } |
183 | |
184 | MUST return the string representing the canonical name of the encoding. |
185 | |
742555bd |
186 | =item -E<gt>mime_name |
187 | |
188 | Predefined As: |
189 | |
190 | sub mime_name{ |
191 | require Encode::MIME::Name; |
192 | return Encode::MIME::Name::get_mime_name(shift->name); |
193 | } |
194 | |
195 | MUST return the string representing the IANA charset name of the encoding. |
196 | |
a0d8a30e |
197 | =item -E<gt>renew |
10c5ecbb |
198 | |
199 | Predefined As: |
200 | |
cc836e95 |
201 | sub renew { |
202 | my $self = shift; |
203 | my $clone = bless { %$self } => ref($self); |
204 | $clone->{renewed}++; |
205 | return $clone; |
206 | } |
a0d8a30e |
207 | |
208 | This method reconstructs the encoding object if necessary. If you need |
209 | to store the state during encoding, this is where you clone your object. |
10c5ecbb |
210 | |
a0d8a30e |
211 | PerlIO ALWAYS calls this method to make sure it has its own private |
212 | encoding object. |
10c5ecbb |
213 | |
cc836e95 |
214 | =item -E<gt>renewed |
215 | |
216 | Predefined As: |
217 | |
218 | sub renewed { $_[0]->{renewed} || 0 } |
219 | |
220 | Tells whether the object is renewed (and how many times). Some |
221 | modules emit C<Use of uninitialized value in null operation> warning |
222 | unless the value is numeric so return 0 for false. |
223 | |
0ab8f81e |
224 | =item -E<gt>perlio_ok() |
225 | |
10c5ecbb |
226 | Predefined As: |
011b2d2f |
227 | |
10c5ecbb |
228 | sub perlio_ok { |
229 | eval{ require PerlIO::encoding }; |
230 | return $@ ? 0 : 1; |
231 | } |
0ab8f81e |
232 | |
10c5ecbb |
233 | If your encoding does not support PerlIO for some reasons, just; |
0ab8f81e |
234 | |
235 | sub perlio_ok { 0 } |
236 | |
237 | =item -E<gt>needs_lines() |
238 | |
10c5ecbb |
239 | Predefined As: |
240 | |
241 | sub needs_lines { 0 }; |
242 | |
0ab8f81e |
243 | If your encoding can work with PerlIO but needs line buffering, you |
244 | MUST define this method so it returns true. 7bit ISO-2022 encodings |
245 | are one example that needs this. When this method is missing, false |
246 | is assumed. |
247 | |
248 | =back |
249 | |
10c5ecbb |
250 | =head2 Example: Encode::ROT13 |
251 | |
252 | package Encode::ROT13; |
253 | use strict; |
254 | use base qw(Encode::Encoding); |
255 | |
256 | __PACKAGE__->Define('rot13'); |
257 | |
258 | sub encode($$;$){ |
259 | my ($obj, $str, $chk) = @_; |
260 | $str =~ tr/A-Za-z/N-ZA-Mn-za-m/; |
261 | $_[1] = '' if $chk; # this is what in-place edit means |
262 | return $str; |
263 | } |
264 | |
265 | # Jr pna or ynml yvxr guvf; |
266 | *decode = \&encode; |
267 | |
268 | 1; |
269 | |
270 | =head1 Why the heck Encode API is different? |
271 | |
0ab8f81e |
272 | It should be noted that the I<$check> behaviour is different from the |
1b2c56c8 |
273 | outer public API. The logic is that the "unchecked" case is useful |
0ab8f81e |
274 | when the encoding is part of a stream which may be reporting errors |
275 | (e.g. STDERR). In such cases, it is desirable to get everything |
1b2c56c8 |
276 | through somehow without causing additional errors which obscure the |
0ab8f81e |
277 | original one. Also, the encoding is best placed to know what the |
1b2c56c8 |
278 | correct replacement character is, so if that is the desired behaviour |
279 | then letting low level code do it is the most efficient. |
280 | |
0ab8f81e |
281 | By contrast, if I<$check> is true, the scheme above allows the |
282 | encoding to do as much as it can and tell the layer above how much |
283 | that was. What is lacking at present is a mechanism to report what |
284 | went wrong. The most likely interface will be an additional method |
285 | call to the object, or perhaps (to avoid forcing per-stream objects |
286 | on otherwise stateless encodings) an additional parameter. |
1b2c56c8 |
287 | |
288 | It is also highly desirable that encoding classes inherit from |
289 | C<Encode::Encoding> as a base class. This allows that class to define |
10c5ecbb |
290 | additional behaviour for all encoding objects. |
1b2c56c8 |
291 | |
292 | package Encode::MyEncoding; |
293 | use base qw(Encode::Encoding); |
294 | |
295 | __PACKAGE__->Define(qw(myCanonical myAlias)); |
296 | |
0ab8f81e |
297 | to create an object with C<< bless {Name => ...}, $class >>, and call |
1b2c56c8 |
298 | define_encoding. They inherit their C<name> method from |
299 | C<Encode::Encoding>. |
300 | |
301 | =head2 Compiled Encodings |
302 | |
0ab8f81e |
303 | For the sake of speed and efficiency, most of the encodings are now |
304 | supported via a I<compiled form>: XS modules generated from UCM |
305 | files. Encode provides the enc2xs tool to achieve that. Please see |
67d7b5ef |
306 | L<enc2xs> for more details. |
1b2c56c8 |
307 | |
67d7b5ef |
308 | =head1 SEE ALSO |
1b2c56c8 |
309 | |
67d7b5ef |
310 | L<perlmod>, L<enc2xs> |
1b2c56c8 |
311 | |
0ab8f81e |
312 | =begin future |
f2a2953c |
313 | |
314 | =over 4 |
315 | |
316 | =item Scheme 1 |
317 | |
0ab8f81e |
318 | The fixup routine gets passed the remaining fragment of string being |
319 | processed. It modifies it in place to remove bytes/characters it can |
320 | understand and returns a string used to represent them. For example: |
f2a2953c |
321 | |
322 | sub fixup { |
323 | my $ch = substr($_[0],0,1,''); |
324 | return sprintf("\x{%02X}",ord($ch); |
325 | } |
326 | |
0ab8f81e |
327 | This scheme is close to how the underlying C code for Encode works, |
328 | but gives the fixup routine very little context. |
f2a2953c |
329 | |
330 | =item Scheme 2 |
331 | |
0ab8f81e |
332 | The fixup routine gets passed the original string, an index into |
333 | it of the problem area, and the output string so far. It appends |
334 | what it wants to the output string and returns a new index into the |
335 | original string. For example: |
f2a2953c |
336 | |
337 | sub fixup { |
338 | # my ($s,$i,$d) = @_; |
339 | my $ch = substr($_[0],$_[1],1); |
340 | $_[2] .= sprintf("\x{%02X}",ord($ch); |
341 | return $_[1]+1; |
342 | } |
343 | |
344 | This scheme gives maximal control to the fixup routine but is more |
0ab8f81e |
345 | complicated to code, and may require that the internals of Encode be tweaked to |
346 | keep the original string intact. |
f2a2953c |
347 | |
348 | =item Other Schemes |
349 | |
0ab8f81e |
350 | Hybrids of the above. |
f2a2953c |
351 | |
352 | Multiple return values rather than in-place modifications. |
353 | |
354 | Index into the string could be C<pos($str)> allowing C<s/\G...//>. |
355 | |
356 | =back |
357 | |
0ab8f81e |
358 | =end future |
359 | |
1b2c56c8 |
360 | =cut |