{
    "mode": "man",
    "parameter": "perlunifaq",
    "section": "1",
    "url": "https://www.chedong.com/phpMan.php/man/perlunifaq/1/json",
    "generated": "2026-06-03T02:53:03Z",
    "sections": {
        "NAME": {
            "content": "perlunifaq - Perl Unicode FAQ\n",
            "subsections": [
                {
                    "name": "Q and A",
                    "content": "This is a list of questions and answers about Unicode in Perl, intended to be read after\nperlunitut.\n"
                },
                {
                    "name": "perlunitut isn't really a Unicode tutorial, is it?",
                    "content": "No, and this isn't really a Unicode FAQ.\n\nPerl has an abstracted interface for all supported character encodings, so this is actually a\ngeneric \"Encode\" tutorial and \"Encode\" FAQ. But many people think that Unicode is special and\nmagical, and I didn't want to disappoint them, so I decided to call the document a Unicode\ntutorial.\n"
                },
                {
                    "name": "What character encodings does Perl support?",
                    "content": "To find out which character encodings your Perl supports, run:\n\nperl -MEncode -le \"print for Encode->encodings(':all')\"\n"
                },
                {
                    "name": "Which version of perl should I use?",
                    "content": "Well, if you can, upgrade to the most recent, but certainly 5.8.1 or newer.  The tutorial and\nFAQ assume the latest release.\n\nYou should also check your modules, and upgrade them if necessary. For example,\nHTML::Entities requires version >= 1.32 to function correctly, even though the changelog is\nsilent about this.\n"
                },
                {
                    "name": "What about binary data, like images?",
                    "content": "Well, apart from a bare \"binmode $fh\", you shouldn't treat them specially.  (The binmode is\nneeded because otherwise Perl may convert line endings on Win32 systems.)\n\nBe careful, though, to never combine text strings with binary strings. If you need text in a\nbinary stream, encode your text strings first using the appropriate encoding, then join them\nwith binary strings. See also: \"What if I don't encode?\".\n"
                },
                {
                    "name": "When should I decode or encode?",
                    "content": "Whenever you're communicating text with anything that is external to your perl process, like\na database, a text file, a socket, or another program. Even if the thing you're communicating\nwith is also written in Perl.\n"
                },
                {
                    "name": "What if I don't decode?",
                    "content": "Whenever your encoded, binary string is used together with a text string, Perl will assume\nthat your binary string was encoded with ISO-8859-1, also known as latin-1. If it wasn't\nlatin-1, then your data is unpleasantly converted. For example, if it was UTF-8, the\nindividual bytes of multibyte characters are seen as separate characters, and then again\nconverted to UTF-8. Such double encoding can be compared to double HTML encoding\n(\"&amp;gt;\"), or double URI encoding (%253E).\n\nThis silent implicit decoding is known as \"upgrading\". That may sound positive, but it's best\nto avoid it.\n"
                },
                {
                    "name": "What if I don't encode?",
                    "content": "It depends on what you output and how you output it.\n\nOutput via a filehandle\n\n•   If the string's characters are all code point 255 or lower, Perl outputs bytes that match\nthose code points. This is what happens with encoded strings. It can also, though, happen\nwith unencoded strings that happen to be all code point 255 or lower.\n\n•   Otherwise, Perl outputs the string encoded as UTF-8. This only happens with strings you\nneglected to encode. Since that should not happen, Perl also throws a \"wide character\"\nwarning in this case.\n\nOther output mechanisms (e.g., \"exec\", \"chdir\", ..)\n\nYour text string will be sent using the bytes in Perl's internal format.\n\nBecause the internal format is often UTF-8, these bugs are hard to spot, because UTF-8 is\nusually the encoding you wanted! But don't be lazy, and don't use the fact that Perl's\ninternal format is UTF-8 to your advantage. Encode explicitly to avoid weird bugs, and to\nshow to maintenance programmers that you thought this through.\n"
                },
                {
                    "name": "Is there a way to automatically decode or encode?",
                    "content": "If all data that comes from a certain handle is encoded in exactly the same way, you can tell\nthe PerlIO system to automatically decode everything, with the \"encoding\" layer. If you do\nthis, you can't accidentally forget to decode or encode anymore, on things that use the\nlayered handle.\n\nYou can provide this layer when \"open\"ing the file:\n\nopen my $fh, '>:encoding(UTF-8)', $filename;  # auto encoding on write\nopen my $fh, '<:encoding(UTF-8)', $filename;  # auto decoding on read\n\nOr if you already have an open filehandle:\n\nbinmode $fh, ':encoding(UTF-8)';\n\nSome database drivers for DBI can also automatically encode and decode, but that is sometimes\nlimited to the UTF-8 encoding.\n"
                },
                {
                    "name": "What if I don't know which encoding was used?",
                    "content": "Do whatever you can to find out, and if you have to: guess. (Don't forget to document your\nguess with a comment.)\n\nYou could open the document in a web browser, and change the character set or character\nencoding until you can visually confirm that all characters look the way they should.\n\nThere is no way to reliably detect the encoding automatically, so if people keep sending you\ndata without charset indication, you may have to educate them.\n"
                },
                {
                    "name": "Can I use Unicode in my Perl sources?",
                    "content": "Yes, you can! If your sources are UTF-8 encoded, you can indicate that with the \"use utf8\"\npragma.\n\nuse utf8;\n\nThis doesn't do anything to your input, or to your output. It only influences the way your\nsources are read. You can use Unicode in string literals, in identifiers (but they still have\nto be \"word characters\" according to \"\\w\"), and even in custom delimiters.\n"
                },
                {
                    "name": "Data::Dumper doesn't restore the UTF8 flag; is it broken?",
                    "content": "No, Data::Dumper's Unicode abilities are as they should be. There have been some complaints\nthat it should restore the UTF8 flag when the data is read again with \"eval\". However, you\nshould really not look at the flag, and nothing indicates that Data::Dumper should break this\nrule.\n\nHere's what happens: when Perl reads in a string literal, it sticks to 8 bit encoding as long\nas it can. (But perhaps originally it was internally encoded as UTF-8, when you dumped it.)\nWhen it has to give that up because other characters are added to the text string, it\nsilently upgrades the string to UTF-8.\n\nIf you properly encode your strings for output, none of this is of your concern, and you can\njust \"eval\" dumped data as always.\n"
                },
                {
                    "name": "Why do regex character classes sometimes match only in the ASCII range?",
                    "content": "Starting in Perl 5.14 (and partially in Perl 5.12), just put a \"use feature\n'unicodestrings'\" near the beginning of your program.  Within its lexical scope you\nshouldn't have this problem.  It also is automatically enabled under \"use feature ':5.12'\" or\n\"use v5.12\" or using \"-E\" on the command line for Perl 5.12 or higher.\n\nThe rationale for requiring this is to not break older programs that rely on the way things\nworked before Unicode came along.  Those older programs knew only about the ASCII character\nset, and so may not work properly for additional characters.  When a string is encoded in\nUTF-8, Perl assumes that the program is prepared to deal with Unicode, but when the string\nisn't, Perl assumes that only ASCII is wanted, and so those characters that are not ASCII\ncharacters aren't recognized as to what they would be in Unicode.  \"use feature\n'unicodestrings'\" tells Perl to treat all characters as Unicode, whether the string is\nencoded in UTF-8 or not, thus avoiding the problem.\n\nHowever, on earlier Perls, or if you pass strings to subroutines outside the feature's scope,\nyou can force Unicode rules by changing the encoding to UTF-8 by doing\n\"utf8::upgrade($string)\". This can be used safely on any string, as it checks and does not\nchange strings that have already been upgraded.\n\nFor a more detailed discussion, see Unicode::Semantics on CPAN.\n"
                },
                {
                    "name": "Why do some characters not uppercase or lowercase correctly?",
                    "content": "See the answer to the previous question.\n"
                },
                {
                    "name": "How can I determine if a string is a text string or a binary string?",
                    "content": "You can't. Some use the UTF8 flag for this, but that's misuse, and makes well behaved modules\nlike Data::Dumper look bad. The flag is useless for this purpose, because it's off when an 8\nbit encoding (by default ISO-8859-1) is used to store the string.\n\nThis is something you, the programmer, has to keep track of; sorry. You could consider\nadopting a kind of \"Hungarian notation\" to help with this.\n"
                },
                {
                    "name": "How do I convert from encoding FOO to encoding BAR?",
                    "content": "By first converting the FOO-encoded byte string to a text string, and then the text string to\na BAR-encoded byte string:\n\nmy $textstring = decode('FOO', $foostring);\nmy $barstring  = encode('BAR', $textstring);\n\nor by skipping the text string part, and going directly from one binary encoding to the\nother:\n\nuse Encode qw(fromto);\nfromto($string, 'FOO', 'BAR');  # changes contents of $string\n\nor by letting automatic decoding and encoding do all the work:\n\nopen my $foofh, '<:encoding(FOO)', 'example.foo.txt';\nopen my $barfh, '>:encoding(BAR)', 'example.bar.txt';\nprint { $barfh } $ while <$foofh>;\n\nWhat are \"decodeutf8\" and \"encodeutf8\"?\nThese are alternate syntaxes for \"decode('utf8', ...)\" and \"encode('utf8', ...)\". Do not use\nthese functions for data exchange. Instead use \"decode('UTF-8', ...)\" and \"encode('UTF-8',\n...)\"; see \"What's the difference between UTF-8 and utf8?\" below.\n"
                },
                {
                    "name": "What is a \"wide character\"?",
                    "content": "This is a term used for characters occupying more than one byte.\n\nThe Perl warning \"Wide character in ...\" is caused by such a character.  With no specified\nencoding layer, Perl tries to fit things into a single byte.  When it can't, it emits this\nwarning (if warnings are enabled), and uses UTF-8 encoded data instead.\n\nTo avoid this warning and to avoid having different output encodings in a single stream,\nalways specify an encoding explicitly, for example with a PerlIO layer:\n\nbinmode STDOUT, \":encoding(UTF-8)\";\n"
                }
            ]
        },
        "INTERNALS": {
            "content": "",
            "subsections": [
                {
                    "name": "What is \"the UTF8 flag\"?",
                    "content": "Please, unless you're hacking the internals, or debugging weirdness, don't think about the\nUTF8 flag at all. That means that you very probably shouldn't use \"isutf8\", \"utf8on\" or\n\"utf8off\" at all.\n\nThe UTF8 flag, also called SvUTF8, is an internal flag that indicates that the current\ninternal representation is UTF-8. Without the flag, it is assumed to be ISO-8859-1. Perl\nconverts between these automatically.  (Actually Perl usually assumes the representation is\nASCII; see \"Why do regex character classes sometimes match only in the ASCII range?\" above.)\n\nOne of Perl's internal formats happens to be UTF-8. Unfortunately, Perl can't keep a secret,\nso everyone knows about this. That is the source of much confusion. It's better to pretend\nthat the internal format is some unknown encoding, and that you always have to encode and\ndecode explicitly.\n"
                },
                {
                    "name": "What about the \"use bytes\" pragma?",
                    "content": "Don't use it. It makes no sense to deal with bytes in a text string, and it makes no sense to\ndeal with characters in a byte string. Do the proper conversions (by decoding/encoding), and\nthings will work out well: you get character counts for decoded data, and byte counts for\nencoded data.\n\n\"use bytes\" is usually a failed attempt to do something useful. Just forget about it.\n"
                },
                {
                    "name": "What about the \"use encoding\" pragma?",
                    "content": "Don't use it. Unfortunately, it assumes that the programmer's environment and that of the\nuser will use the same encoding. It will use the same encoding for the source code and for\nSTDIN and STDOUT. When a program is copied to another machine, the source code does not\nchange, but the STDIO environment might.\n\nIf you need non-ASCII characters in your source code, make it a UTF-8 encoded file and \"use\nutf8\".\n\nIf you need to set the encoding for STDIN, STDOUT, and STDERR, for example based on the\nuser's locale, \"use open\".\n"
                },
                {
                    "name": "What is the difference between \":encoding\" and \":utf8\"?",
                    "content": "Because UTF-8 is one of Perl's internal formats, you can often just skip the encoding or\ndecoding step, and manipulate the UTF8 flag directly.\n\nInstead of \":encoding(UTF-8)\", you can simply use \":utf8\", which skips the encoding step if\nthe data was already represented as UTF8 internally. This is widely accepted as good behavior\nwhen you're writing, but it can be dangerous when reading, because it causes internal\ninconsistency when you have invalid byte sequences. Using \":utf8\" for input can sometimes\nresult in security breaches, so please use \":encoding(UTF-8)\" instead.\n\nInstead of \"decode\" and \"encode\", you could use \"utf8on\" and \"utf8off\", but this is\nconsidered bad style. Especially \"utf8on\" can be dangerous, for the same reason that\n\":utf8\" can.\n\nThere are some shortcuts for oneliners; see -C in perlrun.\n"
                },
                {
                    "name": "What's the difference between \"UTF-8\" and \"utf8\"?",
                    "content": "\"UTF-8\" is the official standard. \"utf8\" is Perl's way of being liberal in what it accepts.\nIf you have to communicate with things that aren't so liberal, you may want to consider using\n\"UTF-8\". If you have to communicate with things that are too liberal, you may have to use\n\"utf8\". The full explanation is in \"UTF-8 vs. utf8 vs. UTF8\" in Encode.\n\n\"UTF-8\" is internally known as \"utf-8-strict\". The tutorial uses UTF-8 consistently, even\nwhere utf8 is actually used internally, because the distinction can be hard to make, and is\nmostly irrelevant.\n\nFor example, utf8 can be used for code points that don't exist in Unicode, like 9999999, but\nif you encode that to UTF-8, you get a substitution character (by default; see \"Handling\nMalformed Data\" in Encode for more ways of dealing with this.)\n\nOkay, if you insist: the \"internal format\" is utf8, not UTF-8. (When it's not some other\nencoding.)\n"
                },
                {
                    "name": "I lost track; what encoding is the internal format really?",
                    "content": "It's good that you lost track, because you shouldn't depend on the internal format being any\nspecific encoding. But since you asked: by default, the internal format is either ISO-8859-1\n(latin-1), or utf8, depending on the history of the string. On EBCDIC platforms, this may be\ndifferent even.\n\nPerl knows how it stored the string internally, and will use that knowledge when you\n\"encode\". In other words: don't try to find out what the internal encoding for a certain\nstring is, but instead just encode it into the encoding that you want.\n"
                }
            ]
        },
        "AUTHOR": {
            "content": "Juerd Waalboer <#####@juerd.nl>\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "perlunicode, perluniintro, Encode\n\n\n\nperl v5.34.0                                 2025-07-25                                PERLUNIFAQ(1)",
            "subsections": []
        }
    },
    "summary": "perlunifaq - Perl Unicode FAQ",
    "flags": [],
    "examples": [],
    "see_also": []
}