Encode::Encoding(3pm)
β¨ Encode::Encoding π
okay bestie, welcome to the Encode::Encoding page from the Perl Programmers Reference Guide β yes that's the whole name, it's giving very official, very "I have a lanyard" energy. this is the base class for encoding implementations and honestly it's kind of the main character of the whole Encode situation. let's get into it π
πΈ NAME πΈ
Encode::Encoding β the Encode Implementation Base Class. that's it, that's the name, she's simple, she's iconic.
π¦ SYNOPSIS π¦
like, literally copy this vibe:
package Encode::MyEncoding;
use parent qw(Encode::Encoding);
__PACKAGE__->Define(qw(myCanonical myAlias));
that's the whole personality test. you inherit, you Define, you slay. β¨
π DESCRIPTION π
so, remember how Encode was like "encodings are objects"? yeah. that. in the current implementation at least, encodings are straight-up objects. the mapping from encoding name β object lives in the %Encode::Encoding hash.
and likeβ¦ you could go poke that hash directly with your grubby little hands, but the docs are begging you β strongly encouraged β to use this base class module instead and add encode() and decode() methods. do it for her. π«Ά
π Methods you should implement
you are strongly encouraged to implement the methods below β at minimum, one of either encode() or decode(). one of them. pick a lane, any lane.
->encode($string [,$check])β MUST return the octet sequence representing$string. details, because details matter:- if
$checkis true, it SHOULD modify$stringin place to remove the converted part (i.e. the whole string, unless there's an error). and ifperlio_ok()is true, that SHOULD becomes an absolute MUST. no cap. π₯ - if an error occurs, it SHOULD return the octet sequence for the fragment of the 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 β MUST again. - if
$checkis false, thenencodeMUST make a "best effort" to convert the string β for example, by using a replacement character. it's trying its best okay π
- if
->decode($octets [,$check])β MUST return the string that$octetsrepresents. same energy, opposite direction:- if
$checkis true, it SHOULD modify$octetsin place to remove the converted part (i.e. the whole sequence, unless there's 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
if you want your encoding to work with the encoding pragma, you should also implement this next one:
->cat_decode($destination, $octets, $offset, $terminator [,$check])β MUST decode$octetswith$offsetand concatenate it to$destination. decoding will terminate when$terminator(a string) appears in the output.$offsetwill be modified to the last$octetsposition at the end of decode. returns true if$terminatorappears in the output, else returns false. she's a little more complicated but she's worth it π
π§ Other methods defined in Encode::Encodings
you do not have to override the methods below unless you have to. like, they're pre-loaded. freebies. take them.
->nameβ predefined as:
sub name { return shift->{'Name'} }
MUST return the string representing the canonical name of the encoding. (it's literally just the Name key, bestie.)
->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 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. always. no exceptions. it's clingy like that.
->renewedβ predefined as:
sub renewed { $_[0]->{renewed} || 0 }
tells whether the object is renewed (and how many times). some modules will emit a Use of uninitialized value in null operation warning unless the value is numeric β so return 0 for false. be a good citizen. πΈ
->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 do this and move on:
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. the 7bit ISO-2022 encodings are one example that needs this. when this method is missing, false is assumed. (so like, default: no line buffering. know that.) β
π Example: Encode::ROT13 π
ok this example ate, honestly:
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;
the *decode = \&encode; is such a flex. one function, two jobs. π
π Why the heck Encode API is different?
great question, bestie, I was also like "huh?" β so here's the tea. β
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's desirable to get everything through somehow without causing additional errors which would obscure the original one. also, the encoding itself is best placed to know what the correct replacement character is β so if that's the behaviour you want, letting the 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's 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. so like, TBD, coming never, who knows π
it's also highly desirable that encoding classes inherit from Encode::Encoding as a base class β because that lets the class define additional behaviour for all encoding objects. so:
package Encode::MyEncoding;
use parent qw(Encode::Encoding);
__PACKAGE__->Define(qw(myCanonical myAlias));
and that creates an object with bless {Name => ...}, $class, and calls define_encoding. they inherit their name method from Encode::Encoding. automatic. effortless. iconic. β¨
π 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 β that's where the real lore lives. π§
π SEE ALSO π
perlmod, enc2xs
and that's the whole page, bestie!! hope you understood it, hope you implement encode and decode, and remember β perlio_ok() being true is when SHOULD becomes MUST, so don't get caught slipping π
this has been Encode::Encoding(3pm) from perl v5.42.3, dated 2026-11-03, brought to you by the Perl Programmers Reference Guide β mwah, love u, bye! ππ¦
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl 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