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