perldoc > Encode::Encoding

📄 NAME

Encode::Encoding - Encode Implementation Base Class

📋 SYNOPSIS

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

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

🚀 Quick Reference

Use CaseCommandDescription
Define a new encoding__PACKAGE__->Define(qw(canonical alias))Register an encoding with name and aliases
Implement encode methodsub encode($$;$){ ... }Convert string to octets, handle $check
Implement decode methodsub decode($$;$){ ... }Convert octets to string, handle $check
Implement cat_decodesub cat_decode($$$$;$){ ... }Decode until terminator appears
Get canonical name$obj->nameReturns the encoding's canonical name
Get IANA charset name$obj->mime_nameReturns the MIME/IANA charset name
Clone encoding object$obj->renewReconstructs a private copy for PerlIO
Check if PerlIO supported$obj->perlio_okReturns true if PerlIO::encoding is available
Check if line buffering needed$obj->needs_linesReturns true if encoding needs line buffering

📖 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().

🛠️ Other methods defined in Encode::Encodings

You do not have to override methods shown below unless you have to.

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

perlmod, enc2xs

Encode::Encoding
📄 NAME 📋 SYNOPSIS 🚀 Quick Reference 📖 DESCRIPTION
🧩 Methods you should implement 🛠️ Other methods defined in Encode::Encodings 📝 Example: Encode::ROT13 ❓ Why the heck Encode API is different? ⚡ Compiled Encodings
📚 SEE ALSO

Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-12 05:10 @216.73.217.145
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^