{
    "mode": "man",
    "parameter": "Encoding",
    "section": "3",
    "url": "https://www.chedong.com/phpMan.php/man/Encoding/3/json",
    "generated": "2026-06-14T00:15:15Z",
    "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\nencoding into UTF-8, and changes the PerlIO layers of \"STDIN\" and \"STDOUT\" to the encoding\nspecified.\n\nThis pragma dates from the days when UTF-8-enabled editors were uncommon.  But that was long\nago, and the need for it is greatly diminished.  That, coupled with the fact that it doesn't\nwork with threads, along with other problems, (see \"BUGS\") have led to its being deprecated.\nIt is planned to remove this pragma in a future Perl version.  New code should be written in\nUTF-8, and the \"use utf8\" pragma used instead (see perluniintro and utf8 for details).  Old\ncode should be converted to UTF-8, via something like the recipe in the \"SYNOPSIS\" (though\nthis simple approach may require manual adjustments afterwards).\n\nIf UTF-8 is not an option, it is recommended that one use a simple source filter, such as\nthat provided 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,\nwith file scope, as the file is likely going to only be written in one encoding.  Further\nrestrictions apply 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\nsource file from the encoding ENCNAME into UTF-8, and similarly converts character code\npoints.  This is used when the script is a combination of ASCII (for the variable names\nand punctuation, etc), but the literal data is in the specified encoding.\n\nENCNAME 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\".\nThis is for very specialized applications, and is documented in \"The \":locale\" sub-\npragma\" below.\n\nThe literals that are converted are \"q//, qq//, qr//, qw///, qx//\", and starting in\nv5.8.1, \"tr///\".  Operations that do conversions include \"chr\", \"ord\", \"utf8::upgrade\"\n(but not \"utf8::downgrade\"), and \"chomp\".\n\nAlso starting in v5.8.1, the \"DATA\" pseudo-filehandle is translated from the encoding\ninto UTF-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\nchange the 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\nallows identifiers in the source to be in that encoding as well.  (Problems may occur if\nthe encoding is not a superset of ASCII; imagine all your semi-colons being translated\ninto something 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\ncan still 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": "",
            "subsections": [
                {
                    "name": "Setting \"STDIN\" and/or \"STDOUT\" individually",
                    "content": "The 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\nbe completely ignored.  See \"\"${^UNICODE}\"\" in perlvar and \"\"-C\"\" in perlrun for details.\n"
                },
                {
                    "name": "The \":locale\" sub-pragma",
                    "content": "Starting in v5.8.6, the encoding name may be \":locale\".  This means that the encoding is\ntaken from the current locale, and not hard-coded by the pragma.  Since a script really can\nonly be encoded in exactly one encoding, this option is dangerous.  It makes sense only if\nthe script itself is written in ASCII, and all the possible locales that will be in use when\nthe script is executed are supersets of ASCII.  That means that the script itself doesn't get\nchanged, but the I/O handles have the specified encoding added, and the operations like \"chr\"\nand \"ord\" use that encoding.\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\nas the default encoding for the open pragma.\n\n2.  If 1. didn't work but we are under the locale pragma, the environment variables \"LCALL\"\nand \"LANG\" (in that order) are matched for encodings (the part after \"\".\"\", if any), and\nif any found, 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)\nare matched for anything looking like UTF-8, and if any found, \":utf8\" is used as the\ndefault encoding for the open pragma.\n\nIf your locale environment variables (\"LCALL\", \"LCCTYPE\", \"LANG\") contain the strings\n'UTF-8' or 'UTF8' (case-insensitive matching), the default encoding of your \"STDIN\",\n\"STDOUT\", and \"STDERR\", and of any subsequent file open, is UTF-8.\n"
                }
            ]
        },
        "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\nthe native 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\nstrings as 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\nwill print 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\nwords, this 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}\"\non the 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\nthe \"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\nto spell your name in a native encoding.  So feel free to put your strings in your encoding\nin quotes 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.\nThis leads 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\nbecause the 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\nso forth.  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\nthe classic idiom that makes \"tr///\" 'interpolate':\n\ntr/$from/$to/;            # wrong!\neval qq{ tr/$from/$to/ }; # workaround.\n"
                },
                {
                    "name": "EXAMPLE - Greekperl",
                    "content": "use 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\nimplementation of the regular expressions may introduce recoding errors for regular\nexpression literals longer 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\nwith it.  When \"format\" contains non-ASCII characters it prints funny or gets \"wide\ncharacter warnings\".  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\nwrite().\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\nideographs) and 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\nspecified above.\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;\nO'Reilly & Associates; ISBN 0-596-00027-8\n\n\n\nperl v5.34.0                                 2022-02-06                                encoding(3pm)",
            "subsections": []
        }
    },
    "summary": "encoding - allows you to write your script in non-ASCII and non-UTF-8",
    "flags": [],
    "examples": [],
    "see_also": []
}