Encode::Encoding(3)
π Encode::Encoding(3) β Encode Implementation Base Class π
User Contributed Perl Documentation, but make it group-chat β¨
π NAME π
Encode::Encoding β Encode Implementation Base Class
π¦ SYNOPSIS π¦
okay so the starter pack is literally this:
package Encode::MyEncoding;
use parent qw(Encode::Encoding);
__PACKAGE__->Define(qw(myCanonical myAlias));
πΈ DESCRIPTION πΈ
okay so real talk: like Encode says, encodings are defined as objects β at least in the current implementation, bestie. The mapping of encoding name to object goes through the %Encode::Encoding hash. You can directly manipulate this hash, but it is strongly encouraged to use this base class module and add encode() and decode() methods. no cap, thatβs the move π
π Methods you should implement π
You are strongly encouraged to implement the methods below, at least either encode() or decode(). Thatβs the assignment, bestie π«Ά
β¨ ->encode($string [,$check]) β¨
MUST return the octet sequence representing $string.
- If
$checkis true, it SHOULD modify$stringin place to remove the converted part (i.e. the whole string unless there is an error). Ifperlio_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
$stringin-place to remove the converted part leaving it starting with the problem fragment. Ifperlio_ok()is true, SHOULD becomes MUST. - If
$checkis false thenencodeMUST make a "best effort" to convert the string β for example, by using a replacement character.
π¦ ->decode($octets [,$check]) π¦
MUST return the string that $octets represents.
- If
$checkis true, it SHOULD modify$octetsin place to remove the converted part (i.e. the whole sequence unless there is an error). Ifperlio_ok()is true, SHOULD becomes MUST. - If an error occurs, it SHOULD return the fragment of string that has been converted and modify
$octetsin-place to remove the converted part leaving it starting with the problem fragment. Ifperlio_ok()is true, SHOULD becomes MUST. - If
$checkis false thendecodeshould 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 pragma, you should also implement the method below. iykyk π
π ->cat_decode($destination, $octets, $offset, $terminator [,$check]) π
MUST decode $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 in output, else returns false.
π«Ά Other methods defined in Encode::Encodings π«Ά
You do not have to override methods shown below unless you have to. no pressure, bestie π
π ->name π
Predefined As:
sub name { return shift->{'Name'} }
MUST return the string representing the canonical name of the encoding.
π ->mime_name π
Predefined As:
sub mime_name{
return Encode::MIME::Name::get_mime_name(shift->name);
}
MUST return the string representing the IANA charset 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 ALWAYS calls this method to make sure it has its own private encoding object. literally always, bestie π
π ->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 {
return eval { require PerlIO::encoding } ? 1 : 0;
}
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 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.
πΈ Example: Encode::ROT13 πΈ
okay this example is iconic, look:
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 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 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".
π₯ Compiled Encodings π₯
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.
π SEE ALSO π
perlmodenc2xs
okay thatβs the whole page, bestie π mwah to perl v5.42.0 and the 2024-03-08 date β source/version and date, signed, sealed, slayed π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Encode 3.210.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Encode 3.210.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Encode 3.210.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Encode 3.210.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms