{
    "mode": "perldoc",
    "parameter": "Encode::PerlIO",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Encode%3A%3APerlIO/json",
    "generated": "2026-06-03T09:45:56Z",
    "sections": {
        "NAME": {
            "content": "Encode::PerlIO -- a detailed document on Encode and PerlIO\n",
            "subsections": []
        },
        "Overview": {
            "content": "It is very common to want to do encoding transformations when reading or writing files, network\nconnections, pipes etc. If Perl is configured to use the new 'perlio' IO system then \"Encode\"\nprovides a \"layer\" (see PerlIO) which can transform data as it is read or written.\n\nHere is how the blind poet would modernise the encoding:\n\nuse Encode;\nopen(my $iliad,'<:encoding(iso-8859-7)','iliad.greek');\nopen(my $utf8,'>:utf8','iliad.utf8');\nmy @epic = <$iliad>;\nprint $utf8 @epic;\nclose($utf8);\nclose($illiad);\n\nIn addition, the new IO system can also be configured to read/write UTF-8 encoded characters (as\nnoted above, this is efficient):\n\nopen(my $fh,'>:utf8','anything');\nprint $fh \"Any \\x{0021} string \\N{SMILEY FACE}\\n\";\n\nEither of the above forms of \"layer\" specifications can be made the default for a lexical scope\nwith the \"use open ...\" pragma. See open.\n\nOnce a handle is open, its layers can be altered using \"binmode\".\n\nWithout any such configuration, or if Perl itself is built using the system's own IO, then write\noperations assume that the file handle accepts only *bytes* and will \"die\" if a character larger\nthan 255 is written to the handle. When reading, each octet from the handle becomes a\nbyte-in-a-character. Note that this default is the same behaviour as bytes-only languages\n(including Perl before v5.6) would have, and is sufficient to handle native 8-bit encodings e.g.\niso-8859-1, EBCDIC etc. and any legacy mechanisms for handling other encodings and binary data.\n\nIn other cases, it is the program's responsibility to transform characters into bytes using the\nAPI above before doing writes, and to transform the bytes read from a handle into characters\nbefore doing \"character operations\" (e.g. \"lc\", \"/\\W+/\", ...).\n\nYou can also use PerlIO to convert larger amounts of data you don't want to bring into memory.\nFor example, to convert between ISO-8859-1 (Latin 1) and UTF-8 (or UTF-EBCDIC in EBCDIC\nmachines):\n\nopen(F, \"<:encoding(iso-8859-1)\", \"data.txt\") or die $!;\nopen(G, \">:utf8\",                 \"data.utf\") or die $!;\nwhile (<F>) { print G }\n\n# Could also do \"print G <F>\" but that would pull\n# the whole file into memory just to write it out again.\n\nMore examples:\n\nopen(my $f, \"<:encoding(cp1252)\")\nopen(my $g, \">:encoding(iso-8859-2)\")\nopen(my $h, \">:encoding(latin9)\")       # iso-8859-15\n\nSee also encoding for how to change the default encoding of the data in your script.\n\nHow does it work?\nHere is a crude diagram of how filehandle, PerlIO, and Encode interact.\n\nfilehandle <-> PerlIO        PerlIO <-> scalar (read/printed)\n\\      /\nEncode\n\nWhen PerlIO receives data from either direction, it fills a buffer (currently with 1024 bytes)\nand passes the buffer to Encode. Encode tries to convert the valid part and passes it back to\nPerlIO, leaving invalid parts (usually a partial character) in the buffer. PerlIO then appends\nmore data to the buffer, calls Encode again, and so on until the data stream ends.\n\nTo do so, PerlIO always calls (de|en)code methods with CHECK set to 1. This ensures that the\nmethod stops at the right place when it encounters partial character. The following is what\nhappens when PerlIO and Encode tries to encode (from utf8) more than 1024 bytes and the buffer\nboundary happens to be in the middle of a character.\n\nA   B   C   ....   ~     \\x{3000}    ....\n41  42  43   ....  7E   e3   80   80  ....\n<- buffer --------------->\n<< encoded >>>>>>>>>>\n<- next buffer ------\n\nEncode converts from the beginning to \\x7E, leaving \\xe3 in the buffer because it is invalid\n(partial character).\n\nUnfortunately, this scheme does not work well with escape-based encodings such as ISO-2022-JP.\n",
            "subsections": []
        },
        "Line Buffering": {
            "content": "Now let's see what happens when you try to decode from ISO-2022-JP and the buffer ends in the\nmiddle of a character.\n\nJIS208-ESC   \\x{5f3e}\nA   B   C   ....   ~   \\e   $   B  |DAN | ....\n41  42  43   ....  7E   1b  24  41  43  46 ....\n<- buffer --------------------------->\n<< encoded >>>>>>>>>>>>>>>>>>>>>>>\n\nAs you see, the next buffer begins with \\x43. But \\x43 is 'C' in ASCII, which is wrong in this\ncase because we are now in JISX 0208 area so it has to convert \\x43\\x46, not \\x43. Unlike utf8\nand EUC, in escape-based encodings you can't tell if a given octet is a whole character or just\npart of it.\n\nFortunately PerlIO also supports line buffer if you tell PerlIO to use one instead of fixed\nbuffer. Since ISO-2022-JP is guaranteed to revert to ASCII at the end of the line, partial\ncharacter will never happen when line buffer is used.\n\nTo tell PerlIO to use line buffer, implement ->needslines method for your encoding object. See\nEncode::Encoding for details.\n\nThanks to these efforts most encodings that come with Encode support PerlIO but that still\nleaves following encodings.\n\niso-2022-kr\nMIME-B\nMIME-Header\nMIME-Q\n\nFortunately iso-2022-kr is hardly used (according to Jungshik) and MIME-* are very unlikely to\nbe fed to PerlIO because they are for mail headers. See Encode::MIME::Header for details.\n\nHow can I tell whether my encoding fully supports PerlIO ?\nAs of this writing, any encoding whose class belongs to Encode::XS and Encode::Unicode works.\nThe Encode module has a \"perliook\" method which you can use before applying PerlIO encoding to\nthe filehandle. Here is an example:\n\nmy $useperlio = perliook($enc);\nmy $layer = $useperlio ? \"<:raw\" : \"<:encoding($enc)\";\nopen my $fh, $layer, $file or die \"$file : $!\";\nwhile(<$fh>){\n$ = decode($enc, $) unless $useperlio;\n# ....\n}\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Encode::Encoding, Encode::Supported, Encode::PerlIO, encoding, perlebcdic, \"open\" in perlfunc,\nperlunicode, utf8, the Perl Unicode Mailing List <perl-unicode@perl.org>\n",
            "subsections": []
        }
    },
    "summary": "Encode::PerlIO -- a detailed document on Encode and PerlIO",
    "flags": [],
    "examples": [],
    "see_also": []
}