{
    "mode": "perldoc",
    "parameter": "Encode::Encoder",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Encode%3A%3AEncoder/json",
    "generated": "2026-06-09T11:40:15Z",
    "synopsis": "use Encode::Encoder;\n# Encode::encode(\"ISO-8859-1\", $data);\nEncode::Encoder->new($data)->iso88591; # OOP way\n# shortcut\nuse Encode::Encoder qw(encoder);\nencoder($data)->iso88591;\n# you can stack them!\nencoder($data)->iso88591->base64;  # provided base64() is defined\n# you can use it as a decoder as well\nencoder($base64)->bytes('base64')->latin1;\n# stringified\nprint encoder($data)->utf8->latin1;  # prints the string in latin1\n# numified\nencoder(\"\\x{abcd}\\x{ef}g\")->utf8 == 6; # true. bytes::length($data)",
    "sections": {
        "NAME": {
            "content": "Encode::Encoder -- Object Oriented Encoder\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Encode::Encoder;\n# Encode::encode(\"ISO-8859-1\", $data);\nEncode::Encoder->new($data)->iso88591; # OOP way\n# shortcut\nuse Encode::Encoder qw(encoder);\nencoder($data)->iso88591;\n# you can stack them!\nencoder($data)->iso88591->base64;  # provided base64() is defined\n# you can use it as a decoder as well\nencoder($base64)->bytes('base64')->latin1;\n# stringified\nprint encoder($data)->utf8->latin1;  # prints the string in latin1\n# numified\nencoder(\"\\x{abcd}\\x{ef}g\")->utf8 == 6; # true. bytes::length($data)\n",
            "subsections": []
        },
        "ABSTRACT": {
            "content": "Encode::Encoder allows you to use Encode in an object-oriented style. This is not only more\nintuitive than a functional approach, but also handier when you want to stack encodings. Suppose\nyou want your UTF-8 string converted to Latin1 then Base64: you can simply say\n\nmy $base64 = encoder($utf8)->latin1->base64;\n\ninstead of\n\nmy $latin1 = encode(\"latin1\", $utf8);\nmy $base64 = encodebase64($utf8);\n\nor the lazier and more convoluted\n\nmy $base64 = encodebase64(encode(\"latin1\", $utf8));\n",
            "subsections": []
        },
        "Description": {
            "content": "Here is how to use this module.\n\n*   There are at least two instance variables stored in a hash reference, {data} and {encoding}.\n\n*   When there is no method, it takes the method name as the name of the encoding and encodes\nthe instance *data* with *encoding*. If successful, the instance *encoding* is set\naccordingly.\n\n*   You can retrieve the result via ->data but usually you don't have to because the stringify\noperator (\"\") is overridden to do exactly that.\n",
            "subsections": [
                {
                    "name": "Predefined Methods",
                    "content": "This module predefines the methods below:\n\n$e = Encode::Encoder->new([$data, $encoding]);\nreturns an encoder object. Its data is initialized with $data if present, and its encoding\nis set to $encoding if present.\n\nWhen $encoding is omitted, it defaults to utf8 if $data is already in utf8 or \"\" (empty\nstring) otherwise.\n"
                },
                {
                    "name": "encoder",
                    "content": "is an alias of Encode::Encoder->new(). This one is exported on demand.\n\n$e->data([$data])\nWhen $data is present, sets the instance data to $data and returns the object itself.\nOtherwise, the current instance data is returned.\n\n$e->encoding([$encoding])\nWhen $encoding is present, sets the instance encoding to $encoding and returns the object\nitself. Otherwise, the current instance encoding is returned.\n\n$e->bytes([$encoding])\ndecodes instance data from $encoding, or the instance encoding if omitted. If the conversion\nis successful, the instance encoding will be set to \"\".\n\nThe name *bytes* was deliberately picked to avoid namespace tainting -- this module may be\nused as a base class so method names that appear in Encode::Encoding are avoided.\n"
                },
                {
                    "name": "Example: base64 transcoder",
                    "content": "This module is designed to work with Encode::Encoding. To make the Base64 transcoder example\nabove really work, you could write a module like this:\n\npackage Encode::Base64;\nuse parent 'Encode::Encoding';\nPACKAGE->Define('base64');\nuse MIME::Base64;\nsub encode{\nmy ($obj, $data) = @;\nreturn encodebase64($data);\n}\nsub decode{\nmy ($obj, $data) = @;\nreturn decodebase64($data);\n}\n1;\nEND\n\nAnd your caller module would be something like this:\n\nuse Encode::Encoder;\nuse Encode::Base64;\n\n# now you can really do the following\n\nencoder($data)->iso88591->base64;\nencoder($base64)->bytes('base64')->latin1;\n"
                },
                {
                    "name": "Operator Overloading",
                    "content": "This module overloads two operators, stringify (\"\") and numify (0+).\n\nStringify dumps the data inside the object.\n\nNumify returns the number of bytes in the instance data.\n\nThey come in handy when you want to print or find the size of data.\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "Encode, Encode::Encoding\n",
            "subsections": []
        }
    },
    "summary": "Encode::Encoder -- Object Oriented Encoder",
    "flags": [],
    "examples": [],
    "see_also": []
}