1bytes(3pm) Perl Programmers Reference Guide bytes(3pm)
2
3
4
6 bytes - Perl pragma to expose the individual bytes of characters
7
9 Because the bytes pragma breaks encapsulation (i.e. it exposes the
10 innards of how the perl executable currently happens to store a
11 string), the byte values that result are in an unspecified encoding.
12
13 Use of this module for anything other than debugging purposes is
14 strongly discouraged. If you feel that the functions here within might
15 be useful for your application, this possibly indicates a mismatch
16 between your mental model of Perl Unicode and the current reality. In
17 that case, you may wish to read some of the perl Unicode documentation:
18 perluniintro, perlunitut, perlunifaq and perlunicode.
19
21 use bytes;
22 ... chr(...); # or bytes::chr
23 ... index(...); # or bytes::index
24 ... length(...); # or bytes::length
25 ... ord(...); # or bytes::ord
26 ... rindex(...); # or bytes::rindex
27 ... substr(...); # or bytes::substr
28 no bytes;
29
31 Perl's characters are stored internally as sequences of one or more
32 bytes. This pragma allows for the examination of the individual bytes
33 that together comprise a character.
34
35 Originally the pragma was designed for the loftier goal of helping
36 incorporate Unicode into Perl, but the approach that used it was found
37 to be defective, and the one remaining legitimate use is for debugging
38 when you need to non-destructively examine characters' individual
39 bytes. Just insert this pragma temporarily, and remove it after the
40 debugging is finished.
41
42 The original usage can be accomplished by explicit (rather than this
43 pragma's implict) encoding using the Encode module:
44
45 use Encode qw/encode/;
46
47 my $utf8_byte_string = encode "UTF8", $string;
48 my $latin1_byte_string = encode "Latin1", $string;
49
50 Or, if performance is needed and you are only interested in the UTF-8
51 representation:
52
53 utf8::encode(my $utf8_byte_string = $string);
54
55 "no bytes" can be used to reverse the effect of "use bytes" within the
56 current lexical scope.
57
58 As an example, when Perl sees "$x = chr(400)", it encodes the character
59 in UTF-8 and stores it in $x. Then it is marked as character data, so,
60 for instance, "length $x" returns 1. However, in the scope of the
61 "bytes" pragma, $x is treated as a series of bytes - the bytes that
62 make up the UTF8 encoding - and "length $x" returns 2:
63
64 $x = chr(400);
65 print "Length is ", length $x, "\n"; # "Length is 1"
66 printf "Contents are %vd\n", $x; # "Contents are 400"
67 {
68 use bytes; # or "require bytes; bytes::length()"
69 print "Length is ", length $x, "\n"; # "Length is 2"
70 printf "Contents are %vd\n", $x; # "Contents are 198.144 (on
71 # ASCII platforms)"
72 }
73
74 "chr()", "ord()", "substr()", "index()" and "rindex()" behave
75 similarly.
76
77 For more on the implications, see perluniintro and perlunicode.
78
79 "bytes::length()" is admittedly handy if you need to know the byte
80 length of a Perl scalar. But a more modern way is:
81
82 use Encode 'encode';
83 length(encode('UTF-8', $scalar))
84
86 "bytes::substr()" does not work as an lvalue().
87
89 perluniintro, perlunicode, utf8, Encode
90
91
92
93perl v5.28.2 2018-11-01 bytes(3pm)