2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 <title>bytes - Perl pragma to force byte semantics rather than character semantics
</title>
6 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
7 <link rev=
"made" href=
"mailto:" />
10 <body style=
"background-color: white">
11 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
12 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
13 <big><strong><span class=
"block"> bytes - Perl pragma to force byte semantics rather than character semantics
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#synopsis">SYNOPSIS
</a></li>
24 <li><a href=
"#description">DESCRIPTION
</a></li>
25 <li><a href=
"#limitations">LIMITATIONS
</a></li>
26 <li><a href=
"#see_also">SEE ALSO
</a></li>
33 <h1><a name=
"name">NAME
</a></h1>
34 <p>bytes - Perl pragma to force byte semantics rather than character semantics
</p>
38 <h1><a name=
"synopsis">SYNOPSIS
</a></h1>
41 ... chr(...); # or bytes::chr
42 ... index(...); # or bytes::index
43 ... length(...); # or bytes::length
44 ... ord(...); # or bytes::ord
45 ... rindex(...); # or bytes::rindex
46 ... substr(...); # or bytes::substr
51 <h1><a name=
"description">DESCRIPTION
</a></h1>
52 <p>The
<code>use bytes
</code> pragma disables character semantics for the rest of the
53 lexical scope in which it appears.
<code>no bytes
</code> can be used to reverse
54 the effect of
<code>use bytes
</code> within the current lexical scope.
</p>
55 <p>Perl normally assumes character semantics in the presence of character
56 data (i.e. data that has come from a source that has been marked as
57 being of a particular character encoding). When
<code>use bytes
</code> is in
58 effect, the encoding is temporarily ignored, and each string is treated
59 as a series of bytes.
</p>
60 <p>As an example, when Perl sees
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_chr"><code>$x = chr(
400)
</code></a>, it encodes the character
61 in UTF-
8 and stores it in $x. Then it is marked as character data, so,
62 for instance,
<code>length $x
</code> returns
<code>1</code>. However, in the scope of the
63 <code>bytes
</code> pragma, $x is treated as a series of bytes - the bytes that make
64 up the UTF8 encoding - and
<code>length $x
</code> returns
<code>2</code>:
</p>
67 print
"Length is
", length $x,
"\n
"; #
"Length is
1"
68 printf
"Contents are %vd\n
", $x; #
"Contents are
400"
70 use bytes; # or
"require bytes; bytes::length()
"
71 print
"Length is
", length $x,
"\n
"; #
"Length is
2"
72 printf
"Contents are %vd\n
", $x; #
"Contents are
198.144"
74 <p>chr(), ord(), substr(),
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_index"><code>index()
</code></a> and
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_rindex"><code>rindex()
</code></a> behave similarly.
</p>
75 <p>For more on the implications and differences between character
76 semantics and byte semantics, see
<a href=
"file://C|\msysgit\mingw\html/pod/perluniintro.html">the perluniintro manpage
</a> and
<a href=
"file://C|\msysgit\mingw\html/pod/perlunicode.html">the perlunicode manpage
</a>.
</p>
80 <h1><a name=
"limitations">LIMITATIONS
</a></h1>
81 <p>bytes::substr() does not work as an lvalue().
</p>
85 <h1><a name=
"see_also">SEE ALSO
</a></h1>
86 <p><a href=
"file://C|\msysgit\mingw\html/pod/perluniintro.html">the perluniintro manpage
</a>,
<a href=
"file://C|\msysgit\mingw\html/pod/perlunicode.html">the perlunicode manpage
</a>,
<a href=
"file://C|\msysgit\mingw\html/lib/utf8.html">the utf8 manpage
</a></p>
87 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
88 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
89 <big><strong><span class=
"block"> bytes - Perl pragma to force byte semantics rather than character semantics
</span></strong></big>