{
    "mode": "perldoc",
    "parameter": "bytes",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/bytes/json",
    "generated": "2026-06-13T20:00:50Z",
    "synopsis": "use bytes;\n... chr(...);       # or bytes::chr\n... index(...);     # or bytes::index\n... length(...);    # or bytes::length\n... ord(...);       # or bytes::ord\n... rindex(...);    # or bytes::rindex\n... substr(...);    # or bytes::substr\nno bytes;",
    "sections": {
        "NAME": {
            "content": "bytes - Perl pragma to expose the individual bytes of characters\n",
            "subsections": []
        },
        "NOTICE": {
            "content": "Because the bytes pragma breaks encapsulation (i.e. it exposes the innards of how the perl\nexecutable currently happens to store a string), the byte values that result are in an\nunspecified encoding.\n\nUse of this module for anything other than debugging purposes is strongly discouraged. If you\nfeel that the functions here within might be useful for your application, this possibly\nindicates a mismatch between your mental model of Perl Unicode and the current reality. In that\ncase, you may wish to read some of the perl Unicode documentation: perluniintro, perlunitut,\nperlunifaq and perlunicode.\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use bytes;\n... chr(...);       # or bytes::chr\n... index(...);     # or bytes::index\n... length(...);    # or bytes::length\n... ord(...);       # or bytes::ord\n... rindex(...);    # or bytes::rindex\n... substr(...);    # or bytes::substr\nno bytes;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Perl's characters are stored internally as sequences of one or more bytes. This pragma allows\nfor the examination of the individual bytes that together comprise a character.\n\nOriginally the pragma was designed for the loftier goal of helping incorporate Unicode into\nPerl, but the approach that used it was found to be defective, and the one remaining legitimate\nuse is for debugging when you need to non-destructively examine characters' individual bytes.\nJust insert this pragma temporarily, and remove it after the debugging is finished.\n\nThe original usage can be accomplished by explicit (rather than this pragma's implicit) encoding\nusing the Encode module:\n\nuse Encode qw/encode/;\n\nmy $utf8bytestring   = encode \"UTF8\",   $string;\nmy $latin1bytestring = encode \"Latin1\", $string;\n\nOr, if performance is needed and you are only interested in the UTF-8 representation:\n\nutf8::encode(my $utf8bytestring = $string);\n\n\"no bytes\" can be used to reverse the effect of \"use bytes\" within the current lexical scope.\n\nAs an example, when Perl sees \"$x = chr(400)\", it encodes the character in UTF-8 and stores it\nin $x. Then it is marked as character data, so, for instance, \"length $x\" returns 1. However, in\nthe scope of the \"bytes\" pragma, $x is treated as a series of bytes - the bytes that make up the\nUTF8 encoding - and \"length $x\" returns 2:\n\n$x = chr(400);\nprint \"Length is \", length $x, \"\\n\";     # \"Length is 1\"\nprintf \"Contents are %vd\\n\", $x;         # \"Contents are 400\"\n{\nuse bytes; # or \"require bytes; bytes::length()\"\nprint \"Length is \", length $x, \"\\n\"; # \"Length is 2\"\nprintf \"Contents are %vd\\n\", $x;     # \"Contents are 198.144 (on\n# ASCII platforms)\"\n}\n\n\"chr()\", \"ord()\", \"substr()\", \"index()\" and \"rindex()\" behave similarly.\n\nFor more on the implications, see perluniintro and perlunicode.\n\n\"bytes::length()\" is admittedly handy if you need to know the byte length of a Perl scalar. But\na more modern way is:\n\nuse Encode 'encode';\nlength(encode('UTF-8', $scalar))\n",
            "subsections": []
        },
        "LIMITATIONS": {
            "content": "\"bytes::substr()\" does not work as an *lvalue()*.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "perluniintro, perlunicode, utf8, Encode\n",
            "subsections": []
        }
    },
    "summary": "bytes - Perl pragma to expose the individual bytes of characters",
    "flags": [],
    "examples": [],
    "see_also": []
}