xref: /openbsd/gnu/usr.bin/perl/lib/bytes.pm (revision 3bef86f7)
1package bytes;
2
3use strict;
4use warnings;
5
6our $VERSION = '1.08';
7
8$bytes::hint_bits = 0x00000008;
9
10sub import {
11    $^H |= $bytes::hint_bits;
12}
13
14sub unimport {
15    $^H &= ~$bytes::hint_bits;
16}
17
18our $AUTOLOAD;
19sub AUTOLOAD {
20    require "bytes_heavy.pl";
21    goto &$AUTOLOAD if defined &$AUTOLOAD;
22    require Carp;
23    Carp::croak("Undefined subroutine $AUTOLOAD called");
24}
25
26sub length (_);
27sub chr (_);
28sub ord (_);
29sub substr ($$;$$);
30sub index ($$;$);
31sub rindex ($$;$);
32
331;
34__END__
35
36=head1 NAME
37
38bytes - Perl pragma to expose the individual bytes of characters
39
40=head1 NOTICE
41
42Because the bytes pragma breaks encapsulation (i.e. it exposes the innards of
43how the perl executable currently happens to store a string), the byte values
44that result are in an unspecified encoding.
45
46B<Use of this module for anything other than debugging purposes is
47strongly discouraged.>  If you feel that the functions here within
48might be useful for your application, this possibly indicates a
49mismatch between your mental model of Perl Unicode and the current
50reality. In that case, you may wish to read some of the perl Unicode
51documentation: L<perluniintro>, L<perlunitut>, L<perlunifaq> and
52L<perlunicode>.
53
54=head1 SYNOPSIS
55
56    use bytes;
57    ... chr(...);       # or bytes::chr
58    ... index(...);     # or bytes::index
59    ... length(...);    # or bytes::length
60    ... ord(...);       # or bytes::ord
61    ... rindex(...);    # or bytes::rindex
62    ... substr(...);    # or bytes::substr
63    no bytes;
64
65
66=head1 DESCRIPTION
67
68Perl's characters are stored internally as sequences of one or more bytes.
69This pragma allows for the examination of the individual bytes that together
70comprise a character.
71
72Originally the pragma was designed for the loftier goal of helping incorporate
73Unicode into Perl, but the approach that used it was found to be defective,
74and the one remaining legitimate use is for debugging when you need to
75non-destructively examine characters' individual bytes.  Just insert this
76pragma temporarily, and remove it after the debugging is finished.
77
78The original usage can be accomplished by explicit (rather than this pragma's
79implicit) encoding using the L<Encode> module:
80
81    use Encode qw/encode/;
82
83    my $utf8_byte_string   = encode "UTF8",   $string;
84    my $latin1_byte_string = encode "Latin1", $string;
85
86Or, if performance is needed and you are only interested in the UTF-8
87representation:
88
89    utf8::encode(my $utf8_byte_string = $string);
90
91C<no bytes> can be used to reverse the effect of C<use bytes> within the
92current lexical scope.
93
94As an example, when Perl sees C<$x = chr(400)>, it encodes the character
95in UTF-8 and stores it in C<$x>. Then it is marked as character data, so,
96for instance, C<length $x> returns C<1>. However, in the scope of the
97C<bytes> pragma, C<$x> is treated as a series of bytes - the bytes that make
98up the UTF8 encoding - and C<length $x> returns C<2>:
99
100 $x = chr(400);
101 print "Length is ", length $x, "\n";     # "Length is 1"
102 printf "Contents are %vd\n", $x;         # "Contents are 400"
103 {
104     use bytes; # or "require bytes; bytes::length()"
105     print "Length is ", length $x, "\n"; # "Length is 2"
106     printf "Contents are %vd\n", $x;     # "Contents are 198.144 (on
107                                          # ASCII platforms)"
108 }
109
110C<chr()>, C<ord()>, C<substr()>, C<index()> and C<rindex()> behave similarly.
111
112For more on the implications, see L<perluniintro> and L<perlunicode>.
113
114C<bytes::length()> is admittedly handy if you need to know the
115B<byte length> of a Perl scalar.  But a more modern way is:
116
117   use Encode 'encode';
118   length(encode('UTF-8', $scalar))
119
120=head1 LIMITATIONS
121
122C<bytes::substr()> does not work as an I<lvalue()>.
123
124=head1 SEE ALSO
125
126L<perluniintro>, L<perlunicode>, L<utf8>, L<Encode>
127
128=cut
129