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 $check is true, it SHOULD modify $string in place to remove the converted part (i.e. the whole string, unless there's an error). and if perlio_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 $string in-place to remove the converted part β€” leaving it starting with the problem fragment. if perlio_ok() is true, SHOULD β†’ MUST again.
    • if $check is false, then encode MUST make a "best effort" to convert the string β€” for example, by using a replacement character. it's trying its best okay 😭
  • ->decode($octets [,$check]) β€” MUST return the string that $octets represents. same energy, opposite direction:
    • if $check is true, it SHOULD modify $octets in place to remove the converted part (i.e. the whole sequence, unless there's an error). if 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 pragma, you should also implement this next one:

  • ->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 the output. $offset will be modified to the last $octets position at the end of decode. returns true if $terminator appears 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