1bytes(3pm) Perl Programmers Reference Guide bytes(3pm)
2
3
4
6 bytes - Perl pragma to force byte semantics rather than character
7 semantics
8
10 This pragma reflects early attempts to incorporate Unicode into perl
11 and has since been superseded. It breaks encapsulation (i.e. it exposes
12 the innards of how the perl executable currently happens to store a
13 string), and use of this module for anything other than debugging
14 purposes is strongly discouraged. If you feel that the functions here
15 within might be useful for your application, this possibly indicates a
16 mismatch between your mental model of Perl Unicode and the current
17 reality. In that case, you may wish to read some of the perl Unicode
18 documentation: 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 The "use bytes" pragma disables character semantics for the rest of the
32 lexical scope in which it appears. "no bytes" can be used to reverse
33 the effect of "use bytes" within the current lexical scope.
34
35 Perl normally assumes character semantics in the presence of character
36 data (i.e. data that has come from a source that has been marked as
37 being of a particular character encoding). When "use bytes" is in
38 effect, the encoding is temporarily ignored, and each string is treated
39 as a series of bytes.
40
41 As an example, when Perl sees "$x = chr(400)", it encodes the character
42 in UTF-8 and stores it in $x. Then it is marked as character data, so,
43 for instance, "length $x" returns 1. However, in the scope of the
44 "bytes" pragma, $x is treated as a series of bytes - the bytes that
45 make up the UTF8 encoding - and "length $x" returns 2:
46
47 $x = chr(400);
48 print "Length is ", length $x, "\n"; # "Length is 1"
49 printf "Contents are %vd\n", $x; # "Contents are 400"
50 {
51 use bytes; # or "require bytes; bytes::length()"
52 print "Length is ", length $x, "\n"; # "Length is 2"
53 printf "Contents are %vd\n", $x; # "Contents are 198.144"
54 }
55
56 chr(), ord(), substr(), index() and rindex() behave similarly.
57
58 For more on the implications and differences between character
59 semantics and byte semantics, see perluniintro and perlunicode.
60
62 bytes::substr() does not work as an lvalue().
63
65 perluniintro, perlunicode, utf8
66
67
68
69perl v5.16.3 2013-02-26 bytes(3pm)