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