The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Encode::Encoding - Encode Implementation Base Class

SYNOPSIS

  package Encode::MyEncoding;
  use base qw(Encode::Encoding);

  __PACKAGE__->Define(qw(myCanonical myAlias));

DESCRIPTION

As mentioned in Encode, encodings are (in the current implementation at least) defined by objects. The mapping of encoding name to object is via the %encodings hash.

The values of the hash can currently be either strings or objects. The string form may go away in the future. The string form occurs when encodings() has scanned @INC for loadable encodings but has not actually loaded the encoding in question. This is because the current "loading" process is all Perl and a bit slow.

Once an encoding is loaded then value of the hash is object which implements the encoding. The object should provide the following interface:

->name

Should return the string representing the canonical name of the encoding.

->new_sequence

This is a placeholder for encodings with state. It should return an object which implements this interface, all current implementations return the original object.

->encode($string,$check)

Should return the octet sequence representing $string. If $check is true it should modify $string in place to remove the converted part (i.e. the whole string unless there is an error). 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 check is is false then encode should make a "best effort" to convert the string - for example by using a replacement character.

->decode($octets,$check)

Should return the string that $octets represents. If $check is true it should modify $octets in place to remove the converted part (i.e. the whole sequence unless there is an error). 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 check is 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.

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 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.

In contrast if check is true, the scheme above allows the encoding to do as much as it can and tell 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) and 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. For example built in Unicode, UCS-2 and UTF-8 classes use :

  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.

Compiled Encodings

For the sake of speed and efficiency, Most of the encodings are now supported via Compiled Form that are XS modules generated from UCM files. Encode provides enc2xs tool to achieve that. Please see enc2xs for more details.

SEE ALSO

perlmod, enc2xs

Scheme 1

Passed remaining fragment of string being processed. Modifies it in place to remove bytes/characters it can understand and returns a string used to represent them. e.g.

 sub fixup {
   my $ch = substr($_[0],0,1,'');
   return sprintf("\x{%02X}",ord($ch);
 }

This scheme is close to how underlying C code for Encode works, but gives the fixup routine very little context.

Scheme 2

Passed original string, and an index into it of the problem area, and output string so far. Appends what it will to output string and returns new index into original string. For example:

 sub fixup {
   # my ($s,$i,$d) = @_;
   my $ch = substr($_[0],$_[1],1);
   $_[2] .= sprintf("\x{%02X}",ord($ch);
   return $_[1]+1;
 }

This scheme gives maximal control to the fixup routine but is more complicated to code, and may need internals of Encode to be tweaked to keep original string intact.

Other Schemes

Hybrids of above.

Multiple return values rather than in-place modifications.

Index into the string could be pos($str) allowing s/\G...//.