1 package Encode::Encoding;
3 # Base class for classes which implement encodings
6 our $VERSION = do { my @r = ( q$Revision: 2.4 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
14 my $canonical = shift;
15 $obj = bless { Name => $canonical }, $obj unless ref $obj;
17 # warn "$canonical => $obj\n";
18 Encode::define_encoding( $obj, $canonical, @_ );
21 sub name { return shift->{'Name'} }
23 # sub renew { return $_[0] }
27 my $clone = bless {%$self} => ref($self);
28 $clone->{renewed}++; # so the caller can see it
29 DEBUG and warn $clone->{renewed};
33 sub renewed { return $_[0]->{renewed} || 0 }
35 *new_sequence = \&renew;
40 eval { require PerlIO::encoding };
44 # (Temporary|legacy) methods
46 sub toUnicode { shift->decode(@_) }
47 sub fromUnicode { shift->encode(@_) }
50 # Needs to be overloaded or just croak
56 my $class = ref($obj) ? ref($obj) : $obj;
57 Carp::croak( $class . "->encode() not defined!" );
63 my $class = ref($obj) ? ref($obj) : $obj;
64 Carp::croak( $class . "->encode() not defined!" );
74 Encode::Encoding - Encode Implementation Base Class
78 package Encode::MyEncoding;
79 use base qw(Encode::Encoding);
81 __PACKAGE__->Define(qw(myCanonical myAlias));
85 As mentioned in L<Encode>, encodings are (in the current
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.
91 =head2 Methods you should implement
93 You are strongly encouraged to implement methods below, at least
94 either encode() or decode().
98 =item -E<gt>encode($string [,$check])
100 MUST return the octet sequence representing I<$string>.
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.
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.
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.
124 =item -E<gt>decode($octets [,$check])
126 MUST return the string that I<$octets> represents.
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.
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.
145 If I<$check> is false then C<decode> should make a "best effort" to
146 convert the string - for example by using Unicode's "\x{FFFD}" as a
147 replacement character.
153 If you want your encoding to work with L<encoding> pragma, you should
154 also implement the method below.
158 =item -E<gt>cat_decode($destination, $octets, $offset, $terminator [,$check])
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.
167 =head2 Other methods defined in Encode::Encodings
169 You do not have to override methods shown below unless you have to.
177 sub name { return shift->{'Name'} }
179 MUST return the string representing the canonical name of the encoding.
187 my $clone = bless { %$self } => ref($self);
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.
195 PerlIO ALWAYS calls this method to make sure it has its own private
202 sub renewed { $_[0]->{renewed} || 0 }
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.
208 =item -E<gt>perlio_ok()
213 eval{ require PerlIO::encoding };
217 If your encoding does not support PerlIO for some reasons, just;
221 =item -E<gt>needs_lines()
225 sub needs_lines { 0 };
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
234 =head2 Example: Encode::ROT13
236 package Encode::ROT13;
238 use base qw(Encode::Encoding);
240 __PACKAGE__->Define('rot13');
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
249 # Jr pna or ynml yvxr guvf;
254 =head1 Why the heck Encode API is different?
256 It should be noted that the I<$check> behaviour is different from the
257 outer public API. The logic is that the "unchecked" case is useful
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
260 through somehow without causing additional errors which obscure the
261 original one. Also, the encoding is best placed to know what the
262 correct replacement character is, so if that is the desired behaviour
263 then letting low level code do it is the most efficient.
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.
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
274 additional behaviour for all encoding objects.
276 package Encode::MyEncoding;
277 use base qw(Encode::Encoding);
279 __PACKAGE__->Define(qw(myCanonical myAlias));
281 to create an object with C<< bless {Name => ...}, $class >>, and call
282 define_encoding. They inherit their C<name> method from
285 =head2 Compiled Encodings
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
290 L<enc2xs> for more details.
294 L<perlmod>, L<enc2xs>
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:
307 my $ch = substr($_[0],0,1,'');
308 return sprintf("\x{%02X}",ord($ch);
311 This scheme is close to how the underlying C code for Encode works,
312 but gives the fixup routine very little context.
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:
322 # my ($s,$i,$d) = @_;
323 my $ch = substr($_[0],$_[1],1);
324 $_[2] .= sprintf("\x{%02X}",ord($ch);
328 This scheme gives maximal control to the fixup routine but is more
329 complicated to code, and may require that the internals of Encode be tweaked to
330 keep the original string intact.
334 Hybrids of the above.
336 Multiple return values rather than in-place modifications.
338 Index into the string could be C<pos($str)> allowing C<s/\G...//>.