Commit | Line | Data |
657b208b |
1 | package bytes; |
5bc28da9 |
2 | |
b75c8c73 |
3 | our $VERSION = '1.00'; |
4 | |
d5448623 |
5 | $bytes::hint_bits = 0x00000008; |
6 | |
5bc28da9 |
7 | sub import { |
d5448623 |
8 | $^H |= $bytes::hint_bits; |
5bc28da9 |
9 | } |
10 | |
11 | sub unimport { |
d5448623 |
12 | $^H &= ~$bytes::hint_bits; |
5bc28da9 |
13 | } |
14 | |
15 | sub AUTOLOAD { |
657b208b |
16 | require "bytes_heavy.pl"; |
5bc28da9 |
17 | goto &$AUTOLOAD; |
18 | } |
19 | |
20 | sub length ($); |
21 | |
22 | 1; |
23 | __END__ |
24 | |
25 | =head1 NAME |
26 | |
657b208b |
27 | bytes - Perl pragma to force byte semantics rather than character semantics |
5bc28da9 |
28 | |
29 | =head1 SYNOPSIS |
30 | |
657b208b |
31 | use bytes; |
32 | no bytes; |
5bc28da9 |
33 | |
34 | =head1 DESCRIPTION |
35 | |
393fec97 |
36 | WARNING: The implementation of Unicode support in Perl is incomplete. |
21bad921 |
37 | See L<perlunicode> for the exact details. |
393fec97 |
38 | |
657b208b |
39 | The C<use bytes> pragma disables character semantics for the rest of the |
40 | lexical scope in which it appears. C<no bytes> can be used to reverse |
41 | the effect of C<use bytes> within the current lexical scope. |
393fec97 |
42 | |
5de28535 |
43 | Perl normally assumes character semantics in the presence of character |
44 | data (i.e. data that has come from a source that has been marked as |
45 | being of a particular character encoding). When C<use bytes> is in |
46 | effect, the encoding is temporarily ignored, and each string is treated |
47 | as a series of bytes. |
48 | |
49 | As an example, when Perl sees C<$x = chr(400)>, it encodes the character |
50 | in UTF8 and stores it in $x. Then it is marked as character data, so, |
51 | for instance, C<length $x> returns C<1>. However, in the scope of the |
52 | C<bytes> pragma, $x is treated as a series of bytes - the bytes that make |
53 | up the UTF8 encoding - and C<length $x> returns C<2>: |
54 | |
55 | $x = chr(400); |
56 | print "Length is ", length $x, "\n"; # "Length is 1" |
57 | printf "Contents are %vd\n", $x; # "Contents are 400" |
58 | { |
59 | use bytes; |
60 | print "Length is ", length $x, "\n"; # "Length is 2" |
61 | printf "Contents are %vd\n", $x; # "Contents are 198.144" |
62 | } |
63 | |
64 | For more on the implications and differences between character |
393fec97 |
65 | semantics and byte semantics, see L<perlunicode>. |
66 | |
67 | =head1 SEE ALSO |
68 | |
69 | L<perlunicode>, L<utf8> |
5bc28da9 |
70 | |
71 | =cut |