Debian Codegraph @

upstream/debian-unstable/man3/bytes.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "bytes 3perl"
58 .TH bytes 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 bytes \- Perl pragma to expose the individual bytes of characters
65 .SH NOTICE
66 .IX Header "NOTICE"
67 Because the bytes pragma breaks encapsulation (i.e. it exposes the innards of
68 how the perl executable currently happens to store a string), the byte values
69 that result are in an unspecified encoding.
70 .PP
71 \&\fBUse of this module for anything other than debugging purposes is
72 strongly discouraged.\fR If you feel that the functions here within
73 might be useful for your application, this possibly indicates a
74 mismatch between your mental model of Perl Unicode and the current
75 reality. In that case, you may wish to read some of the perl Unicode
76 documentation: perluniintro, perlunitut, perlunifaq and
77 perlunicode.
78 .SH SYNOPSIS
79 .IX Header "SYNOPSIS"
80 .Vb 8
81 \& use bytes;
82 \& ... chr(...); # or bytes::chr
83 \& ... index(...); # or bytes::index
84 \& ... length(...); # or bytes::length
85 \& ... ord(...); # or bytes::ord
86 \& ... rindex(...); # or bytes::rindex
87 \& ... substr(...); # or bytes::substr
88 \& no bytes;
89 .Ve
90 .SH DESCRIPTION
91 .IX Header "DESCRIPTION"
92 Perl's characters are stored internally as sequences of one or more bytes.
93 This pragma allows for the examination of the individual bytes that together
94 comprise a character.
95 .PP
96 Originally the pragma was designed for the loftier goal of helping incorporate
97 Unicode into Perl, but the approach that used it was found to be defective,
98 and the one remaining legitimate use is for debugging when you need to
99 non-destructively examine characters' individual bytes. Just insert this
100 pragma temporarily, and remove it after the debugging is finished.
101 .PP
102 The original usage can be accomplished by explicit (rather than this pragma's
103 implicit) encoding using the Encode module:
104 .PP
105 .Vb 1
106 \& use Encode qw/encode/;
107 \&
108 \& my $utf8_byte_string = encode "UTF8", $string;
109 \& my $latin1_byte_string = encode "Latin1", $string;
110 .Ve
111 .PP
112 Or, if performance is needed and you are only interested in the UTF\-8
113 representation:
114 .PP
115 .Vb 1
116 \& utf8::encode(my $utf8_byte_string = $string);
117 .Ve
118 .PP
119 \&\f(CW\*(C`no bytes\*(C'\fR can be used to reverse the effect of \f(CW\*(C`use bytes\*(C'\fR within the
120 current lexical scope.
121 .PP
122 As an example, when Perl sees \f(CW\*(C`$x = chr(400)\*(C'\fR, it encodes the character
123 in UTF\-8 and stores it in \f(CW$x\fR. Then it is marked as character data, so,
124 for instance, \f(CW\*(C`length $x\*(C'\fR returns \f(CW1\fR. However, in the scope of the
125 \&\f(CW\*(C`bytes\*(C'\fR pragma, \f(CW$x\fR is treated as a series of bytes \- the bytes that make
126 up the UTF8 encoding \- and \f(CW\*(C`length $x\*(C'\fR returns \f(CW2\fR:
127 .PP
128 .Vb 9
129 \& $x = chr(400);
130 \& print "Length is ", length $x, "\en"; # "Length is 1"
131 \& printf "Contents are %vd\en", $x; # "Contents are 400"
132 \& {
133 \& use bytes; # or "require bytes; bytes::length()"
134 \& print "Length is ", length $x, "\en"; # "Length is 2"
135 \& printf "Contents are %vd\en", $x; # "Contents are 198.144 (on
136 \& # ASCII platforms)"
137 \& }
138 .Ve
139 .PP
140 \&\f(CWchr()\fR, \f(CWord()\fR, \f(CWsubstr()\fR, \f(CWindex()\fR and \f(CWrindex()\fR behave similarly.
141 .PP
142 For more on the implications, see perluniintro and perlunicode.
143 .PP
144 \&\f(CWbytes::length()\fR is admittedly handy if you need to know the
145 \&\fBbyte length\fR of a Perl scalar. But a more modern way is:
146 .PP
147 .Vb 2
148 \& use Encode \*(Aqencode\*(Aq;
149 \& length(encode(\*(AqUTF\-8\*(Aq, $scalar))
150 .Ve
151 .SH LIMITATIONS
152 .IX Header "LIMITATIONS"
153 \&\f(CWbytes::substr()\fR does not work as an \fR\f(BIlvalue()\fR\fI\fR.
154 .SH "SEE ALSO"
155 .IX Header "SEE ALSO"
156 perluniintro, perlunicode, utf8, Encode