{
    "mode": "perldoc",
    "parameter": "encoding",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/encoding/json",
    "generated": "2026-06-14T04:15:01Z",
    "synopsis": "use encoding \"greek\";  # Perl like Greek to you?\nuse encoding \"euc-jp\"; # Jperl!\n# or you can even do this if your shell supports your native encoding\nperl -Mencoding=latin2 -e'...' # Feeling centrally European?\nperl -Mencoding=euc-kr -e'...' # Or Korean?\n# more control\n# A simple euc-cn => utf-8 converter\nuse encoding \"euc-cn\", STDOUT => \"utf8\";  while(<>){print};\n# \"no encoding;\" supported\nno encoding;\n# an alternate way, Filter\nuse encoding \"euc-jp\", Filter=>1;\n# now you can use kanji identifiers -- in euc-jp!\n# encode based on the current locale - specialized purposes only;\n# fraught with danger!!\nuse encoding ':locale';",
    "sections": {
        "NAME": {
            "content": "encoding - allows you to write your script in non-ASCII and non-UTF-8\n",
            "subsections": []
        },
        "WARNING": {
            "content": "This module has been deprecated since perl v5.18. See \"DESCRIPTION\" and \"BUGS\".\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use encoding \"greek\";  # Perl like Greek to you?\nuse encoding \"euc-jp\"; # Jperl!\n\n# or you can even do this if your shell supports your native encoding\n\nperl -Mencoding=latin2 -e'...' # Feeling centrally European?\nperl -Mencoding=euc-kr -e'...' # Or Korean?\n\n# more control\n\n# A simple euc-cn => utf-8 converter\nuse encoding \"euc-cn\", STDOUT => \"utf8\";  while(<>){print};\n\n# \"no encoding;\" supported\nno encoding;\n\n# an alternate way, Filter\nuse encoding \"euc-jp\", Filter=>1;\n# now you can use kanji identifiers -- in euc-jp!\n\n# encode based on the current locale - specialized purposes only;\n# fraught with danger!!\nuse encoding ':locale';\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This pragma is used to enable a Perl script to be written in encodings that aren't strictly\nASCII nor UTF-8. It translates all or portions of the Perl program script from a given encoding\ninto UTF-8, and changes the PerlIO layers of \"STDIN\" and \"STDOUT\" to the encoding specified.\n\nThis pragma dates from the days when UTF-8-enabled editors were uncommon. But that was long ago,\nand the need for it is greatly diminished. That, coupled with the fact that it doesn't work with\nthreads, along with other problems, (see \"BUGS\") have led to its being deprecated. It is planned\nto remove this pragma in a future Perl version. New code should be written in UTF-8, and the\n\"use utf8\" pragma used instead (see perluniintro and utf8 for details). Old code should be\nconverted to UTF-8, via something like the recipe in the \"SYNOPSIS\" (though this simple approach\nmay require manual adjustments afterwards).\n\nIf UTF-8 is not an option, it is recommended that one use a simple source filter, such as that\nprovided by Filter::Encoding on CPAN or this pragma's own \"Filter\" option (see below).\n\nThe only legitimate use of this pragma is almost certainly just one per file, near the top, with\nfile scope, as the file is likely going to only be written in one encoding. Further restrictions\napply in Perls before v5.22 (see \"Prior to Perl v5.22\").\n\nThere are two basic modes of operation (plus turning if off):\n\n\"use encoding ['*ENCNAME*'] ;\"\nPlease note: This mode of operation is no longer supported as of Perl v5.26.\n\nThis is the normal operation. It translates various literals encountered in the Perl source\nfile from the encoding *ENCNAME* into UTF-8, and similarly converts character code points.\nThis is used when the script is a combination of ASCII (for the variable names and\npunctuation, *etc*), but the literal data is in the specified encoding.\n\n*ENCNAME* is optional. If omitted, the encoding specified in the environment variable\n\"PERLENCODING\" is used. If this isn't set, or the resolved-to encoding is not known to\n\"Encode\", the error \"Unknown encoding '*ENCNAME*'\" will be thrown.\n\nStarting in Perl v5.8.6 (\"Encode\" version 2.0.1), *ENCNAME* may be the name \":locale\". This\nis for very specialized applications, and is documented in \"The \":locale\" sub-pragma\" below.\n\nThe literals that are converted are \"q//, qq//, qr//, qw///, qx//\", and starting in v5.8.1,\n\"tr///\". Operations that do conversions include \"chr\", \"ord\", \"utf8::upgrade\" (but not\n\"utf8::downgrade\"), and \"chomp\".\n\nAlso starting in v5.8.1, the \"DATA\" pseudo-filehandle is translated from the encoding into\nUTF-8.\n\nFor example, you can write code in EUC-JP as follows:\n\nmy $Rakuda = \"\\xF1\\xD1\\xF1\\xCC\"; # Camel in Kanji\n#<-char-><-char->   # 4 octets\ns/\\bCamel\\b/$Rakuda/;\n\nAnd with \"use encoding \"euc-jp\"\" in effect, it is the same thing as that code in UTF-8:\n\nmy $Rakuda = \"\\x{99F1}\\x{99DD}\"; # two Unicode Characters\ns/\\bCamel\\b/$Rakuda/;\n\nSee \"EXAMPLE\" below for a more complete example.\n\nUnless \"${^UNICODE}\" (available starting in v5.8.2) exists and is non-zero, the PerlIO\nlayers of \"STDIN\" and \"STDOUT\" are set to \"\":encoding(*ENCNAME*)\"\". Therefore,\n\nuse encoding \"euc-jp\";\nmy $message = \"Camel is the symbol of perl.\\n\";\nmy $Rakuda = \"\\xF1\\xD1\\xF1\\xCC\"; # Camel in Kanji\n$message =~ s/\\bCamel\\b/$Rakuda/;\nprint $message;\n\nwill print\n\n\"\\xF1\\xD1\\xF1\\xCC is the symbol of perl.\\n\"\n\nnot\n\n\"\\x{99F1}\\x{99DD} is the symbol of perl.\\n\"\n\nYou can override this by giving extra arguments; see below.\n\nNote that \"STDERR\" WILL NOT be changed, regardless.\n\nAlso note that non-STD file handles remain unaffected. Use \"use open\" or \"binmode\" to change\nthe layers of those.\n\n\"use encoding *ENCNAME*, Filter=>1;\"\nThis operates as above, but the \"Filter\" argument with a non-zero value causes the entire\nscript, and not just literals, to be translated from the encoding into UTF-8. This allows\nidentifiers in the source to be in that encoding as well. (Problems may occur if the\nencoding is not a superset of ASCII; imagine all your semi-colons being translated into\nsomething different.) One can use this form to make\n\n${\"\\x{4eba}\"}++\n\nwork. (This is equivalent to \"$*human*++\", where *human* is a single Han ideograph).\n\nThis effectively means that your source code behaves as if it were written in UTF-8 with\n\"'use utf8\"' in effect. So even if your editor only supports ShiftJIS, for example, you can\nstill try examples in Chapter 15 of \"Programming Perl, 3rd Ed.\".\n\nThis option is significantly slower than the other one.\n\n\"no encoding;\"\nUnsets the script encoding. The layers of \"STDIN\", \"STDOUT\" are reset to \"\":raw\"\" (the\ndefault unprocessed raw stream of bytes).\n",
            "subsections": []
        },
        "OPTIONS": {
            "content": "Setting \"STDIN\" and/or \"STDOUT\" individually\nThe encodings of \"STDIN\" and \"STDOUT\" are individually settable by parameters to the pragma:\n\nuse encoding 'euc-tw', STDIN => 'greek'  ...;\n\nIn this case, you cannot omit the first *ENCNAME*. \"STDIN => undef\" turns the I/O transcoding\ncompletely off for that filehandle.\n\nWhen \"${^UNICODE}\" (available starting in v5.8.2) exists and is non-zero, these options will be\ncompletely ignored. See \"\"${^UNICODE}\"\" in perlvar and \"\"-C\"\" in perlrun for details.\n\nThe \":locale\" sub-pragma\nStarting in v5.8.6, the encoding name may be \":locale\". This means that the encoding is taken\nfrom the current locale, and not hard-coded by the pragma. Since a script really can only be\nencoded in exactly one encoding, this option is dangerous. It makes sense only if the script\nitself is written in ASCII, and all the possible locales that will be in use when the script is\nexecuted are supersets of ASCII. That means that the script itself doesn't get changed, but the\nI/O handles have the specified encoding added, and the operations like \"chr\" and \"ord\" use that\nencoding.\n\nThe logic of finding which locale \":locale\" uses is as follows:\n\n1.  If the platform supports the \"langinfo(CODESET)\" interface, the codeset returned is used as\nthe default encoding for the open pragma.\n\n2.  If 1. didn't work but we are under the locale pragma, the environment variables \"LCALL\" and\n\"LANG\" (in that order) are matched for encodings (the part after \"\".\"\", if any), and if any\nfound, that is used as the default encoding for the open pragma.\n\n3.  If 1. and 2. didn't work, the environment variables \"LCALL\" and \"LANG\" (in that order) are\nmatched for anything looking like UTF-8, and if any found, \":utf8\" is used as the default\nencoding for the open pragma.\n\nIf your locale environment variables (\"LCALL\", \"LCCTYPE\", \"LANG\") contain the strings 'UTF-8'\nor 'UTF8' (case-insensitive matching), the default encoding of your \"STDIN\", \"STDOUT\", and\n\"STDERR\", and of any subsequent file open, is UTF-8.\n",
            "subsections": []
        },
        "CAVEATS": {
            "content": "SIDE EFFECTS\n*   If the \"encoding\" pragma is in scope then the lengths returned are calculated from the\nlength of $/ in Unicode characters, which is not always the same as the length of $/ in the\nnative encoding.\n\n*   Without this pragma, if strings operating under byte semantics and strings with Unicode\ncharacter data are concatenated, the new string will be created by decoding the byte strings\nas *ISO 8859-1 (Latin-1)*.\n\nThe encoding pragma changes this to use the specified encoding instead. For example:\n\nuse encoding 'utf8';\nmy $string = chr(20000); # a Unicode string\nutf8::encode($string);   # now it's a UTF-8 encoded byte string\n# concatenate with another Unicode string\nprint length($string . chr(20000));\n\nWill print 2, because $string is upgraded as UTF-8. Without \"use encoding 'utf8';\", it will\nprint 4 instead, since $string is three octets when interpreted as Latin-1.\n\nDO NOT MIX MULTIPLE ENCODINGS\nNotice that only literals (string or regular expression) having only legacy code points are\naffected: if you mix data like this\n\n\\x{100}\\xDF\n\\xDF\\x{100}\n\nthe data is assumed to be in (Latin 1 and) Unicode, not in your native encoding. In other words,\nthis will match in \"greek\":\n\n\"\\xDF\" =~ /\\x{3af}/\n\nbut this will not\n\n\"\\xDF\\x{100}\" =~ /\\x{3af}\\x{100}/\n\nsince the \"\\xDF\" (ISO 8859-7 GREEK SMALL LETTER IOTA WITH TONOS) on the left will not be\nupgraded to \"\\x{3af}\" (Unicode GREEK SMALL LETTER IOTA WITH TONOS) because of the \"\\x{100}\" on\nthe left. You should not be mixing your legacy data and Unicode in the same string.\n\nThis pragma also affects encoding of the 0x80..0xFF code point range: normally characters in\nthat range are left as eight-bit bytes (unless they are combined with characters with code\npoints 0x100 or larger, in which case all characters need to become UTF-8 encoded), but if the\n\"encoding\" pragma is present, even the 0x80..0xFF range always gets UTF-8 encoded.\n\nAfter all, the best thing about this pragma is that you don't have to resort to \\x{....} just to\nspell your name in a native encoding. So feel free to put your strings in your encoding in\nquotes and regexes.\n",
            "subsections": [
                {
                    "name": "Prior to Perl v5.22",
                    "content": "The pragma was a per script, not a per block lexical. Only the last \"use encoding\" or \"no\nencoding\" mattered, and it affected the whole script. However, the \"no encoding\" pragma was\nsupported and \"use encoding\" could appear as many times as you want in a given script (though\nonly the last was effective).\n\nSince the scope wasn't lexical, other modules' use of \"chr\", \"ord\", *etc.* were affected. This\nleads to spooky, incorrect action at a distance that is hard to debug.\n\nThis means you would have to be very careful of the load order:\n\n# called module\npackage ModuleINBAR;\nuse encoding \"bar\";\n# stuff in \"bar\" encoding here\n1;\n\n# caller script\nuse encoding \"foo\"\nuse ModuleINBAR;\n# surprise! use encoding \"bar\" is in effect.\n\nThe best way to avoid this oddity is to use this pragma RIGHT AFTER other modules are loaded.\ni.e.\n\nuse ModuleINBAR;\nuse encoding \"foo\";\n"
                },
                {
                    "name": "Prior to Encode version 1.87",
                    "content": "*   \"STDIN\" and \"STDOUT\" were not set under the filter option. And \"STDIN=>*ENCODING*\" and\n\"STDOUT=>*ENCODING*\" didn't work like non-filter version.\n\n*   \"use utf8\" wasn't implicitly declared so you have to \"use utf8\" to do\n\n${\"\\x{4eba}\"}++\n"
                },
                {
                    "name": "Prior to Perl v5.8.1",
                    "content": "\"NON-EUC\" doublebyte encodings\nBecause perl needs to parse the script before applying this pragma, such encodings as\nShiftJIS and Big-5 that may contain '\\' (BACKSLASH; \"\\x5c\") in the second byte fail because\nthe second byte may accidentally escape the quoting character that follows.\n\n\"tr///\"\nThe encoding pragma works by decoding string literals in \"q//,qq//,qr//,qw///, qx//\" and so\nforth. In perl v5.8.0, this does not apply to \"tr///\". Therefore,\n\nuse encoding 'euc-jp';\n#....\n$kana =~ tr/\\xA4\\xA1-\\xA4\\xF3/\\xA5\\xA1-\\xA5\\xF3/;\n#           -------- -------- -------- --------\n\nDoes not work as\n\n$kana =~ tr/\\x{3041}-\\x{3093}/\\x{30a1}-\\x{30f3}/;\n\nLegend of characters above\nutf8     euc-jp   charnames::viacode()\n-----------------------------------------\n\\x{3041} \\xA4\\xA1 HIRAGANA LETTER SMALL A\n\\x{3093} \\xA4\\xF3 HIRAGANA LETTER N\n\\x{30a1} \\xA5\\xA1 KATAKANA LETTER SMALL A\n\\x{30f3} \\xA5\\xF3 KATAKANA LETTER N\n\nThis counterintuitive behavior has been fixed in perl v5.8.1.\n\nIn perl v5.8.0, you can work around this as follows;\n\nuse encoding 'euc-jp';\n#  ....\neval qq{ \\$kana =~ tr/\\xA4\\xA1-\\xA4\\xF3/\\xA5\\xA1-\\xA5\\xF3/ };\n\nNote the \"tr//\" expression is surrounded by \"qq{}\". The idea behind this is the same as the\nclassic idiom that makes \"tr///\" 'interpolate':\n\ntr/$from/$to/;            # wrong!\neval qq{ tr/$from/$to/ }; # workaround.\n\nEXAMPLE - Greekperl\nuse encoding \"iso 8859-7\";\n\n# \\xDF in ISO 8859-7 (Greek) is \\x{3af} in Unicode.\n\n$a = \"\\xDF\";\n$b = \"\\x{100}\";\n\nprintf \"%#x\\n\", ord($a); # will print 0x3af, not 0xdf\n\n$c = $a . $b;\n\n# $c will be \"\\x{3af}\\x{100}\", not \"\\x{df}\\x{100}\".\n\n# chr() is affected, and ...\n\nprint \"mega\\n\"  if ord(chr(0xdf)) == 0x3af;\n\n# ... ord() is affected by the encoding pragma ...\n\nprint \"tera\\n\" if ord(pack(\"C\", 0xdf)) == 0x3af;\n\n# ... as are eq and cmp ...\n\nprint \"peta\\n\" if \"\\x{3af}\" eq  pack(\"C\", 0xdf);\nprint \"exa\\n\"  if \"\\x{3af}\" cmp pack(\"C\", 0xdf) == 0;\n\n# ... but pack/unpack C are not affected, in case you still\n# want to go back to your native encoding\n\nprint \"zetta\\n\" if unpack(\"C\", (pack(\"C\", 0xdf))) == 0xdf;\n"
                }
            ]
        },
        "BUGS": {
            "content": "Thread safety\n\"use encoding ...\" is not thread-safe (i.e., do not use in threaded applications).\n\nCan't be used by more than one module in a single program.\nOnly one encoding is allowed. If you combine modules in a program that have different\nencodings, only one will be actually used.\n\nOther modules using \"STDIN\" and \"STDOUT\" get the encoded stream\nThey may be expecting something completely different.\n\nliterals in regex that are longer than 127 bytes\nFor native multibyte encodings (either fixed or variable length), the current implementation\nof the regular expressions may introduce recoding errors for regular expression literals\nlonger than 127 bytes.\n\nEBCDIC\nThe encoding pragma is not supported on EBCDIC platforms.\n\n\"format\"\nThis pragma doesn't work well with \"format\" because PerlIO does not get along very well with\nit. When \"format\" contains non-ASCII characters it prints funny or gets \"wide character\nwarnings\". To understand it, try the code below.\n\n# Save this one in utf8\n# replace *non-ascii* with a non-ascii string\nmy $camel;\nformat STDOUT =\n*non-ascii*@>>>>>>>\n$camel\n.\n$camel = \"*non-ascii*\";\nbinmode(STDOUT=>':encoding(utf8)'); # bang!\nwrite;              # funny\nprint $camel, \"\\n\"; # fine\n\nWithout binmode this happens to work but without binmode, print() fails instead of write().\n\nAt any rate, the very use of \"format\" is questionable when it comes to unicode characters\nsince you have to consider such things as character width (i.e. double-width for ideographs)\nand directions (i.e. BIDI for Arabic and Hebrew).\n\nSee also \"CAVEATS\"\n",
            "subsections": []
        },
        "HISTORY": {
            "content": "This pragma first appeared in Perl v5.8.0. It has been enhanced in later releases as specified\nabove.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "perlunicode, Encode, open, Filter::Util::Call,\n\nCh. 15 of \"Programming Perl (3rd Edition)\" by Larry Wall, Tom Christiansen, Jon Orwant; O'Reilly\n& Associates; ISBN 0-596-00027-8\n",
            "subsections": []
        }
    },
    "summary": "encoding - allows you to write your script in non-ASCII and non-UTF-8",
    "flags": [],
    "examples": [],
    "see_also": []
}