Encode::Encoding - Encode Implementation Base Class |
Encode::Encoding - Encode Implementation Base Class
package Encode::MyEncoding; use base qw(Encode::Encoding);
__PACKAGE__->Define(qw(myCanonical myAlias));
As mentioned in the Encode manpage, encodings are (in the current
implementation at least) defined as objects. The mapping of encoding
name to object is via the %Encode::Encoding
hash. Though you can
directly manipulate this hash, it is strongly encouraged to use this
base class module and add encode()
and decode()
methods.
You are strongly encouraged to implement methods below, at least
either encode()
or decode().
perlio_ok()
is true, SHOULD becomes MUST.
If an error occurs, it SHOULD return the octet sequence for the
fragment of string that has been converted and modify $string in-place
to remove the converted part leaving it starting with the problem
fragment. If perlio_ok()
is true, SHOULD becomes MUST.
If $check is is false then encode
MUST make a ``best effort'' to
convert the string - for example, by using a replacement character.
perlio_ok()
is true, SHOULD becomes MUST.
If an error occurs, it SHOULD return the fragment of string that has
been converted and modify $octets in-place to remove the converted
part leaving it starting with the problem fragment. If perlio_ok()
is
true, SHOULD becomes MUST.
If $check is false then decode
should make a ``best effort'' to
convert the string - for example by using Unicode's ``\x{FFFD}'' as a
replacement character.
If you want your encoding to work with the encoding manpage pragma, you should also implement the method below.
You do not have to override methods shown below unless you have to.
sub name { return shift->{'Name'} }
MUST return the string representing the canonical name of the encoding.
sub renew { return $_[0] }
This method reconstructs the encoding object if necessary. If you need to store the state during encoding, this is where you clone your object. Here is an example:
sub renew { my $self = shift; my $clone = bless { %$self } => ref($self); $clone->{clone} = 1; # so the caller can see it return $clone; }
Since most encodings are stateless the default behavior is just return itself as shown above.
PerlIO ALWAYS calls this method to make sure it has its own private encoding object.
perlio_ok()
sub perlio_ok { eval{ require PerlIO::encoding }; return $@ ? 0 : 1; }
If your encoding does not support PerlIO for some reasons, just;
sub perlio_ok { 0 }
needs_lines()
sub needs_lines { 0 };
If your encoding can work with PerlIO but needs line buffering, you MUST define this method so it returns true. 7bit ISO-2022 encodings are one example that needs this. When this method is missing, false is assumed.
package Encode::ROT13; use strict; use base qw(Encode::Encoding);
__PACKAGE__->Define('rot13');
sub encode($$;$){ my ($obj, $str, $chk) = @_; $str =~ tr/A-Za-z/N-ZA-Mn-za-m/; $_[1] = '' if $chk; # this is what in-place edit means return $str; }
# Jr pna or ynml yvxr guvf; *decode = \&encode;
1;
It should be noted that the $check behaviour is different from the outer public API. The logic is that the ``unchecked'' case is useful when the encoding is part of a stream which may be reporting errors (e.g. STDERR). In such cases, it is desirable to get everything through somehow without causing additional errors which obscure the original one. Also, the encoding is best placed to know what the correct replacement character is, so if that is the desired behaviour then letting low level code do it is the most efficient.
By contrast, if $check is true, the scheme above allows the encoding to do as much as it can and tell the layer above how much that was. What is lacking at present is a mechanism to report what went wrong. The most likely interface will be an additional method call to the object, or perhaps (to avoid forcing per-stream objects on otherwise stateless encodings) an additional parameter.
It is also highly desirable that encoding classes inherit from
Encode::Encoding
as a base class. This allows that class to define
additional behaviour for all encoding objects.
package Encode::MyEncoding; use base qw(Encode::Encoding);
__PACKAGE__->Define(qw(myCanonical myAlias));
to create an object with bless {Name => ...}, $class
, and call
define_encoding. They inherit their name
method from
Encode::Encoding
.
For the sake of speed and efficiency, most of the encodings are now supported via a compiled form: XS modules generated from UCM files. Encode provides the enc2xs tool to achieve that. Please see enc2xs for more details.
the perlmod manpage, enc2xs
Encode::Encoding - Encode Implementation Base Class |