Upgrade to Encode 2.18
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / Encoding.pm
CommitLineData
18586f54 1package Encode::Encoding;
d1256cb1 2
18586f54 3# Base class for classes which implement encodings
4use strict;
656ebd29 5use warnings;
6our $VERSION = do { my @r = ( q$Revision: 2.4 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
621b0f8d 7
8require Encode;
18586f54 9
cc836e95 10sub DEBUG { 0 }
d1256cb1 11
12sub 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 21sub name { return shift->{'Name'} }
a0d8a30e 22
cc836e95 23# sub renew { return $_[0] }
24
25sub 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 33sub renewed { return $_[0]->{renewed} || 0 }
cc836e95 34
a0d8a30e 35*new_sequence = \&renew;
10c5ecbb 36
d1256cb1 37sub needs_lines { 0 }
10c5ecbb 38
d1256cb1 39sub perlio_ok {
40 eval { require PerlIO::encoding };
10c5ecbb 41 return $@ ? 0 : 1;
42}
18586f54 43
a0d8a30e 44# (Temporary|legacy) methods
45
d1256cb1 46sub toUnicode { shift->decode(@_) }
47sub fromUnicode { shift->encode(@_) }
18586f54 48
10c5ecbb 49#
50# Needs to be overloaded or just croak
51#
18586f54 52
10c5ecbb 53sub 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 60sub 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 67sub DESTROY { }
284ee456 68
18586f54 691;
70__END__
1b2c56c8 71
72=head1 NAME
73
74Encode::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
85As mentioned in L<Encode>, encodings are (in the current
10c5ecbb 86implementation at least) defined as objects. The mapping of encoding
87name to object is via the C<%Encode::Encoding> hash. Though you can
88directly manipulate this hash, it is strongly encouraged to use this
89base class module and add encode() and decode() methods.
1b2c56c8 90
10c5ecbb 91=head2 Methods you should implement
1b2c56c8 92
10c5ecbb 93You are strongly encouraged to implement methods below, at least
94either encode() or decode().
1b2c56c8 95
96=over 4
97
10c5ecbb 98=item -E<gt>encode($string [,$check])
1b2c56c8 99
0ab8f81e 100MUST return the octet sequence representing I<$string>.
101
102=over 2
103
104=item *
105
106If I<$check> is true, it SHOULD modify I<$string> in place to remove
107the converted part (i.e. the whole string unless there is an error).
108If perlio_ok() is true, SHOULD becomes MUST.
109
110=item *
111
112If an error occurs, it SHOULD return the octet sequence for the
113fragment of string that has been converted and modify $string in-place
114to remove the converted part leaving it starting with the problem
115fragment. If perlio_ok() is true, SHOULD becomes MUST.
116
117=item *
1b2c56c8 118
0ab8f81e 119If I<$check> is is false then C<encode> MUST make a "best effort" to
120convert 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 126MUST return the string that I<$octets> represents.
127
128=over 2
129
130=item *
131
132If I<$check> is true, it SHOULD modify I<$octets> in place to remove
133the converted part (i.e. the whole sequence unless there is an
134error). If perlio_ok() is true, SHOULD becomes MUST.
135
136=item *
1b2c56c8 137
0ab8f81e 138If an error occurs, it SHOULD return the fragment of string that has
139been converted and modify $octets in-place to remove the converted
140part leaving it starting with the problem fragment. If perlio_ok() is
141true, SHOULD becomes MUST.
142
143=item *
144
145If I<$check> is false then C<decode> should make a "best effort" to
1b2c56c8 146convert the string - for example by using Unicode's "\x{FFFD}" as a
147replacement character.
148
149=back
150
8676e7d3 151=back
152
153If you want your encoding to work with L<encoding> pragma, you should
154also implement the method below.
155
156=over 4
157
220e2d4e 158=item -E<gt>cat_decode($destination, $octets, $offset, $terminator [,$check])
159
160MUST decode I<$octets> with I<$offset> and concatenate it to I<$destination>.
161Decoding will terminate when $terminator (a string) appears in output.
162I<$offset> will be modified to the last $octets position at end of decode.
163Returns true if $terminator appears output, else returns false.
164
151b5d36 165=back
166
10c5ecbb 167=head2 Other methods defined in Encode::Encodings
168
169You do not have to override methods shown below unless you have to.
170
171=over 4
172
173=item -E<gt>name
174
175Predefined As:
176
177 sub name { return shift->{'Name'} }
178
179MUST return the string representing the canonical name of the encoding.
180
a0d8a30e 181=item -E<gt>renew
10c5ecbb 182
183Predefined 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
192This method reconstructs the encoding object if necessary. If you need
193to store the state during encoding, this is where you clone your object.
10c5ecbb 194
a0d8a30e 195PerlIO ALWAYS calls this method to make sure it has its own private
196encoding object.
10c5ecbb 197
cc836e95 198=item -E<gt>renewed
199
200Predefined As:
201
202 sub renewed { $_[0]->{renewed} || 0 }
203
204Tells whether the object is renewed (and how many times). Some
205modules emit C<Use of uninitialized value in null operation> warning
206unless the value is numeric so return 0 for false.
207
0ab8f81e 208=item -E<gt>perlio_ok()
209
10c5ecbb 210Predefined As:
011b2d2f 211
10c5ecbb 212 sub perlio_ok {
213 eval{ require PerlIO::encoding };
214 return $@ ? 0 : 1;
215 }
0ab8f81e 216
10c5ecbb 217If 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 223Predefined As:
224
225 sub needs_lines { 0 };
226
0ab8f81e 227If your encoding can work with PerlIO but needs line buffering, you
228MUST define this method so it returns true. 7bit ISO-2022 encodings
229are one example that needs this. When this method is missing, false
230is 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 256It should be noted that the I<$check> behaviour is different from the
1b2c56c8 257outer public API. The logic is that the "unchecked" case is useful
0ab8f81e 258when 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 260through somehow without causing additional errors which obscure the
0ab8f81e 261original one. Also, the encoding is best placed to know what the
1b2c56c8 262correct replacement character is, so if that is the desired behaviour
263then letting low level code do it is the most efficient.
264
0ab8f81e 265By contrast, if I<$check> is true, the scheme above allows the
266encoding to do as much as it can and tell the layer above how much
267that was. What is lacking at present is a mechanism to report what
268went wrong. The most likely interface will be an additional method
269call to the object, or perhaps (to avoid forcing per-stream objects
270on otherwise stateless encodings) an additional parameter.
1b2c56c8 271
272It is also highly desirable that encoding classes inherit from
273C<Encode::Encoding> as a base class. This allows that class to define
10c5ecbb 274additional 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 281to create an object with C<< bless {Name => ...}, $class >>, and call
1b2c56c8 282define_encoding. They inherit their C<name> method from
283C<Encode::Encoding>.
284
285=head2 Compiled Encodings
286
0ab8f81e 287For the sake of speed and efficiency, most of the encodings are now
288supported via a I<compiled form>: XS modules generated from UCM
289files. Encode provides the enc2xs tool to achieve that. Please see
67d7b5ef 290L<enc2xs> for more details.
1b2c56c8 291
67d7b5ef 292=head1 SEE ALSO
1b2c56c8 293
67d7b5ef 294L<perlmod>, L<enc2xs>
1b2c56c8 295
0ab8f81e 296=begin future
f2a2953c 297
298=over 4
299
300=item Scheme 1
301
0ab8f81e 302The fixup routine gets passed the remaining fragment of string being
303processed. It modifies it in place to remove bytes/characters it can
304understand 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 311This scheme is close to how the underlying C code for Encode works,
312but gives the fixup routine very little context.
f2a2953c 313
314=item Scheme 2
315
0ab8f81e 316The fixup routine gets passed the original string, an index into
317it of the problem area, and the output string so far. It appends
318what it wants to the output string and returns a new index into the
319original 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
328This scheme gives maximal control to the fixup routine but is more
0ab8f81e 329complicated to code, and may require that the internals of Encode be tweaked to
330keep the original string intact.
f2a2953c 331
332=item Other Schemes
333
0ab8f81e 334Hybrids of the above.
f2a2953c 335
336Multiple return values rather than in-place modifications.
337
338Index into the string could be C<pos($str)> allowing C<s/\G...//>.
339
340=back
341
0ab8f81e 342=end future
343
1b2c56c8 344=cut