1bytes(3pm)             Perl Programmers Reference Guide             bytes(3pm)
2
3
4

NAME

6       bytes - Perl pragma to force byte semantics rather than character
7       semantics
8

SYNOPSIS

10           use bytes;
11           ... chr(...);       # or bytes::chr
12           ... index(...);     # or bytes::index
13           ... length(...);    # or bytes::length
14           ... ord(...);       # or bytes::ord
15           ... rindex(...);    # or bytes::rindex
16           ... substr(...);    # or bytes::substr
17           no bytes;
18

DESCRIPTION

20       The "use bytes" pragma disables character semantics for the rest of the
21       lexical scope in which it appears.  "no bytes" can be used to reverse
22       the effect of "use bytes" within the current lexical scope.
23
24       Perl normally assumes character semantics in the presence of character
25       data (i.e. data that has come from a source that has been marked as
26       being of a particular character encoding). When "use bytes" is in
27       effect, the encoding is temporarily ignored, and each string is treated
28       as a series of bytes.
29
30       As an example, when Perl sees "$x = chr(400)", it encodes the character
31       in UTF-8 and stores it in $x. Then it is marked as character data, so,
32       for instance, "length $x" returns 1. However, in the scope of the
33       "bytes" pragma, $x is treated as a series of bytes - the bytes that
34       make up the UTF8 encoding - and "length $x" returns 2:
35
36           $x = chr(400);
37           print "Length is ", length $x, "\n";     # "Length is 1"
38           printf "Contents are %vd\n", $x;         # "Contents are 400"
39           {
40               use bytes; # or "require bytes; bytes::length()"
41               print "Length is ", length $x, "\n"; # "Length is 2"
42               printf "Contents are %vd\n", $x;     # "Contents are 198.144"
43           }
44
45       chr(), ord(), substr(), index() and rindex() behave similarly.
46
47       For more on the implications and differences between character semanā€
48       tics and byte semantics, see perluniintro and perlunicode.
49

LIMITATIONS

51       bytes::substr() does not work as an lvalue().
52

SEE ALSO

54       perluniintro, perlunicode, utf8
55
56
57
58perl v5.8.8                       2001-09-21                        bytes(3pm)
Impressum