{
    "mode": "perldoc",
    "parameter": "Encode::Encoding",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Encode%3A%3AEncoding/json",
    "generated": "2026-06-03T07:31:07Z",
    "synopsis": "package Encode::MyEncoding;\nuse parent qw(Encode::Encoding);\nPACKAGE->Define(qw(myCanonical myAlias));",
    "sections": {
        "NAME": {
            "content": "Encode::Encoding - Encode Implementation Base Class\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package Encode::MyEncoding;\nuse parent qw(Encode::Encoding);\n\nPACKAGE->Define(qw(myCanonical myAlias));\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "As mentioned in Encode, encodings are (in the current implementation at least) defined as\nobjects. The mapping of encoding name to object is via the %Encode::Encoding hash. Though you\ncan directly manipulate this hash, it is strongly encouraged to use this base class module and\nadd encode() and decode() methods.\n",
            "subsections": [
                {
                    "name": "Methods you should implement",
                    "content": "You are strongly encouraged to implement methods below, at least either encode() or decode().\n\n->encode($string [,$check])\nMUST return the octet sequence representing *$string*.\n\n* If *$check* is true, it SHOULD modify *$string* in place to remove the converted part\n(i.e. the whole string unless there is an error). If perliook() is true, SHOULD becomes\nMUST.\n\n* If an error occurs, it SHOULD return the octet sequence for the fragment of string that\nhas been converted and modify $string in-place to remove the converted part leaving it\nstarting with the problem fragment. If perliook() is true, SHOULD becomes MUST.\n\n* If *$check* is false then \"encode\" MUST make a \"best effort\" to convert the string - for\nexample, by using a replacement character.\n\n->decode($octets [,$check])\nMUST return the string that *$octets* represents.\n\n* If *$check* is true, it SHOULD modify *$octets* in place to remove the converted part\n(i.e. the whole sequence unless there is an error). If perliook() is true, SHOULD becomes\nMUST.\n\n* If an error occurs, it SHOULD return the fragment of string that has been converted and\nmodify $octets in-place to remove the converted part leaving it starting with the problem\nfragment. If perliook() is true, SHOULD becomes MUST.\n\n* If *$check* is false then \"decode\" should make a \"best effort\" to convert the string - for\nexample by using Unicode's \"\\x{FFFD}\" as a replacement character.\n\nIf you want your encoding to work with encoding pragma, you should also implement the method\nbelow.\n\n->catdecode($destination, $octets, $offset, $terminator [,$check])\nMUST decode *$octets* with *$offset* and concatenate it to *$destination*. Decoding will\nterminate when $terminator (a string) appears in output. *$offset* will be modified to the\nlast $octets position at end of decode. Returns true if $terminator appears output, else\nreturns false.\n"
                },
                {
                    "name": "Other methods defined in Encode::Encodings",
                    "content": "You do not have to override methods shown below unless you have to.\n\n->name\nPredefined As:\n\nsub name  { return shift->{'Name'} }\n\nMUST return the string representing the canonical name of the encoding.\n\n->mimename\nPredefined As:\n\nsub mimename{\nreturn Encode::MIME::Name::getmimename(shift->name);\n}\n\nMUST return the string representing the IANA charset name of the encoding.\n\n->renew\nPredefined As:\n\nsub renew {\nmy $self = shift;\nmy $clone = bless { %$self } => ref($self);\n$clone->{renewed}++;\nreturn $clone;\n}\n\nThis method reconstructs the encoding object if necessary. If you need to store the state\nduring encoding, this is where you clone your object.\n\nPerlIO ALWAYS calls this method to make sure it has its own private encoding object.\n\n->renewed\nPredefined As:\n\nsub renewed { $[0]->{renewed} || 0 }\n\nTells whether the object is renewed (and how many times). Some modules emit \"Use of\nuninitialized value in null operation\" warning unless the value is numeric so return 0 for\nfalse.\n\n->perliook()\nPredefined As:\n\nsub perliook {\nreturn eval { require PerlIO::encoding } ? 1 : 0;\n}\n\nIf your encoding does not support PerlIO for some reasons, just;\n\nsub perliook { 0 }\n\n->needslines()\nPredefined As:\n\nsub needslines { 0 };\n\nIf your encoding can work with PerlIO but needs line buffering, you MUST define this method\nso it returns true. 7bit ISO-2022 encodings are one example that needs this. When this\nmethod is missing, false is assumed.\n"
                },
                {
                    "name": "Example: Encode::ROT13",
                    "content": "package Encode::ROT13;\nuse strict;\nuse parent qw(Encode::Encoding);\n\nPACKAGE->Define('rot13');\n\nsub encode($$;$){\nmy ($obj, $str, $chk) = @;\n$str =~ tr/A-Za-z/N-ZA-Mn-za-m/;\n$[1] = '' if $chk; # this is what in-place edit means\nreturn $str;\n}\n\n# Jr pna or ynml yvxr guvf;\n*decode = \\&encode;\n\n1;\n\nWhy the heck Encode API is different?\nIt should be noted that the *$check* behaviour is different from the outer public API. The logic\nis that the \"unchecked\" case is useful when the encoding is part of a stream which may be\nreporting errors (e.g. STDERR). In such cases, it is desirable to get everything through somehow\nwithout causing additional errors which obscure the original one. Also, the encoding is best\nplaced to know what the correct replacement character is, so if that is the desired behaviour\nthen letting low level code do it is the most efficient.\n\nBy contrast, if *$check* is true, the scheme above allows the encoding to do as much as it can\nand tell the layer above how much that was. What is lacking at present is a mechanism to report\nwhat went wrong. The most likely interface will be an additional method call to the object, or\nperhaps (to avoid forcing per-stream objects on otherwise stateless encodings) an additional\nparameter.\n\nIt is also highly desirable that encoding classes inherit from \"Encode::Encoding\" as a base\nclass. This allows that class to define additional behaviour for all encoding objects.\n\npackage Encode::MyEncoding;\nuse parent qw(Encode::Encoding);\n\nPACKAGE->Define(qw(myCanonical myAlias));\n\nto create an object with \"bless {Name => ...}, $class\", and call defineencoding. They inherit\ntheir \"name\" method from \"Encode::Encoding\".\n"
                },
                {
                    "name": "Compiled Encodings",
                    "content": "For the sake of speed and efficiency, most of the encodings are now supported via a *compiled\nform*: XS modules generated from UCM files. Encode provides the enc2xs tool to achieve that.\nPlease see enc2xs for more details.\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "perlmod, enc2xs\n",
            "subsections": []
        }
    },
    "summary": "Encode::Encoding - Encode Implementation Base Class",
    "flags": [],
    "examples": [],
    "see_also": []
}