Encode::Encoding (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
Encode::Encoding - Encode Implementation Base ClassSYNOPSIS
package Encode::MyEncoding; use parent qw(Encode::Encoding); __PACKAGE__->Define(qw(myCanonical myAlias));
DESCRIPTION
As mentioned in Encode, 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.Methods you should implement
You are strongly encouraged to implement methods below, at least either encode() or decode().- ->encode($string [,$check])
-
MUSTreturn the octet sequence representing $string.
-
- *
-
If $check is true, it SHOULDmodify $string in place to remove the converted part (i.e. the whole string unless there is an error). If perlio_ok() is true,SHOULDbecomesMUST.
- *
-
If an error occurs, it SHOULDreturn 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,SHOULDbecomesMUST.
- *
-
If $check is false then "encode" MUSTmake a ``best effort'' to convert the string - for example, by using a replacement character.
-
- ->decode($octets [,$check])
-
MUSTreturn the string that $octets represents.
-
- *
-
If $check is true, it SHOULDmodify $octets in place to remove the converted part (i.e. the whole sequence unless there is an error). If perlio_ok() is true,SHOULDbecomesMUST.
- *
-
If an error occurs, it SHOULDreturn 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,SHOULDbecomesMUST.
- *
-
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 encoding pragma, you should also implement the method below.
- ->cat_decode($destination, $octets, $offset, $terminator [,$check])
-
MUSTdecode $octets with $offset and concatenate it to $destination. Decoding will terminate when $terminator (a string) appears in output. $offset will be modified to the last $octets position at end of decode. Returns true if $terminator appears output, else returns false.
Other methods defined in Encode::Encodings
You do not have to override methods shown below unless you have to.- ->name
-
Predefined As:
sub name { return shift->{'Name'} }
MUSTreturn the string representing the canonical name of the encoding. - ->mime_name
-
Predefined As:
sub mime_name{ require Encode::MIME::Name; return Encode::MIME::Name::get_mime_name(shift->name); }
MUSTreturn the string representing theIANAcharset name of the encoding. - ->renew
-
Predefined As:
sub renew { my $self = shift; my $clone = bless { %$self } => ref($self); $clone->{renewed}++; return $clone; }
This method reconstructs the encoding object if necessary. If you need to store the state during encoding, this is where you clone your object.
PerlIO
ALWAYScalls this method to make sure it has its own private encoding object. - ->renewed
-
Predefined As:
sub renewed { $_[0]->{renewed} || 0 }
Tells whether the object is renewed (and how many times). Some modules emit "Use of uninitialized value in null operation" warning unless the value is numeric so return 0 for false.
- ->perlio_ok()
-
Predefined As:
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()
-
Predefined As:
sub needs_lines { 0 };
If your encoding can work with PerlIO but needs line buffering, you
MUSTdefine this method so it returns true. 7bitISO-2022encodings are one example that needs this. When this method is missing, false is assumed.
Example: Encode::ROT13
package Encode::ROT13; use strict; use parent 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;
Why the heck Encode API is different?
It should be noted that the $check behaviour is different from the outer publicBy 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 parent 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".