Debian Codegraph @

upstream/fedora-44/man1/perlhacktut.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (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 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "PERLHACKTUT 1"
61 .TH PERLHACKTUT 1 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 perlhacktut \- Walk through the creation of a simple C code patch
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 This document takes you through a simple patch example.
71 .PP
72 If you haven\*(Aqt read perlhack yet, go do that first! You might also
73 want to read through perlsource too.
74 .PP
75 Once you\*(Aqre done here, check out perlhacktips next.
76 .SH "EXAMPLE OF A SIMPLE PATCH"
77 .IX Header "EXAMPLE OF A SIMPLE PATCH"
78 Let\*(Aqs take a simple patch from start to finish.
79 .PP
80 Here\*(Aqs something Larry suggested: if a \f(CW\*(C`U\*(C'\fR is the first active format
81 during a \f(CW\*(C`pack\*(C'\fR, (for example, \f(CW\*(C`pack "U3C8", @stuff\*(C'\fR) then the
82 resulting string should be treated as UTF\-8 encoded.
83 .PP
84 If you are working with a git clone of the Perl repository, you will
85 want to create a branch for your changes. This will make creating a
86 proper patch much simpler. See the perlgit for details on how to do
87 this.
88 .SS "Writing the patch"
89 .IX Subsection "Writing the patch"
90 How do we prepare to fix this up? First we locate the code in question
91 \&\- the \f(CW\*(C`pack\*(C'\fR happens at runtime, so it\*(Aqs going to be in one of the
92 \&\fIpp\fR files. Sure enough, \f(CW\*(C`pp_pack\*(C'\fR is in \fIpp.c\fR. Since we\*(Aqre going
93 to be altering this file, let\*(Aqs copy it to \fIpp.c~\fR.
94 .PP
95 [Well, it was in \fIpp.c\fR when this tutorial was written. It has now
96 been split off with \f(CW\*(C`pp_unpack\*(C'\fR to its own file, \fIpp_pack.c\fR]
97 .PP
98 Now let\*(Aqs look over \f(CW\*(C`pp_pack\*(C'\fR: we take a pattern into \f(CW\*(C`pat\*(C'\fR, and then
99 loop over the pattern, taking each format character in turn into
100 \&\f(CW\*(C`datum_type\*(C'\fR. Then for each possible format character, we swallow up
101 the other arguments in the pattern (a field width, an asterisk, and so
102 on) and convert the next chunk input into the specified format, adding
103 it onto the output SV \f(CW\*(C`cat\*(C'\fR.
104 .PP
105 How do we know if the \f(CW\*(C`U\*(C'\fR is the first format in the \f(CW\*(C`pat\*(C'\fR? Well, if
106 we have a pointer to the start of \f(CW\*(C`pat\*(C'\fR then, if we see a \f(CW\*(C`U\*(C'\fR we can
107 test whether we\*(Aqre still at the start of the string. So, here\*(Aqs where
108 \&\f(CW\*(C`pat\*(C'\fR is set up:
109 .PP
110 .Vb 6
111 \& STRLEN fromlen;
112 \& char *pat = SvPVx(*++MARK, fromlen);
113 \& char *patend = pat + fromlen;
114 \& I32 len;
115 \& I32 datumtype;
116 \& SV *fromstr;
117 .Ve
118 .PP
119 We\*(Aqll have another string pointer in there:
120 .PP
121 .Vb 7
122 \& STRLEN fromlen;
123 \& char *pat = SvPVx(*++MARK, fromlen);
124 \& char *patend = pat + fromlen;
125 \& + char *patcopy;
126 \& I32 len;
127 \& I32 datumtype;
128 \& SV *fromstr;
129 .Ve
130 .PP
131 And just before we start the loop, we\*(Aqll set \f(CW\*(C`patcopy\*(C'\fR to be the start
132 of \f(CW\*(C`pat\*(C'\fR:
133 .PP
134 .Vb 5
135 \& items = SP \- MARK;
136 \& MARK++;
137 \& SvPVCLEAR(cat);
138 \& + patcopy = pat;
139 \& while (pat < patend) {
140 .Ve
141 .PP
142 Now if we see a \f(CW\*(C`U\*(C'\fR which was at the start of the string, we turn on
143 the \f(CW\*(C`UTF8\*(C'\fR flag for the output SV, \f(CW\*(C`cat\*(C'\fR:
144 .PP
145 .Vb 5
146 \& + if (datumtype == \*(AqU\*(Aq && pat==patcopy+1)
147 \& + SvUTF8_on(cat);
148 \& if (datumtype == \*(Aq#\*(Aq) {
149 \& while (pat < patend && *pat != \*(Aq\en\*(Aq)
150 \& pat++;
151 .Ve
152 .PP
153 Remember that it has to be \f(CW\*(C`patcopy+1\*(C'\fR because the first character of
154 the string is the \f(CW\*(C`U\*(C'\fR which has been swallowed into \f(CW\*(C`datumtype!\*(C'\fR
155 .PP
156 Oops, we forgot one thing: what if there are spaces at the start of the
157 pattern? \f(CW\*(C`pack(" U*", @stuff)\*(C'\fR will have \f(CW\*(C`U\*(C'\fR as the first active
158 character, even though it\*(Aqs not the first thing in the pattern. In this
159 case, we have to advance \f(CW\*(C`patcopy\*(C'\fR along with \f(CW\*(C`pat\*(C'\fR when we see
160 spaces:
161 .PP
162 .Vb 2
163 \& if (isSPACE(datumtype))
164 \& continue;
165 .Ve
166 .PP
167 needs to become
168 .PP
169 .Vb 4
170 \& if (isSPACE(datumtype)) {
171 \& patcopy++;
172 \& continue;
173 \& }
174 .Ve
175 .PP
176 OK. That\*(Aqs the C part done. Now we must do two additional things before
177 this patch is ready to go: we\*(Aqve changed the behaviour of Perl, and so
178 we must document that change. We must also provide some more regression
179 tests to make sure our patch works and doesn\*(Aqt create a bug somewhere
180 else along the line.
181 .SS "Testing the patch"
182 .IX Subsection "Testing the patch"
183 The regression tests for each operator live in \fIt/op/\fR, and so we make
184 a copy of \fIt/op/pack.t\fR to \fIt/op/pack.t~\fR. Now we can add our tests
185 to the end. First, we\*(Aqll test that the \f(CW\*(C`U\*(C'\fR does indeed create Unicode
186 strings.
187 .PP
188 t/op/pack.t has a sensible \fBok()\fR function, but if it didn\*(Aqt we could use
189 the one from t/test.pl.
190 .PP
191 .Vb 2
192 \& require \*(Aq./test.pl\*(Aq;
193 \& plan( tests => 159 );
194 .Ve
195 .PP
196 so instead of this:
197 .PP
198 .Vb 3
199 \& print \*(Aqnot \*(Aq unless "1.20.300.4000" eq sprintf "%vd",
200 \& pack("U*",1,20,300,4000);
201 \& print "ok $test\en"; $test++;
202 .Ve
203 .PP
204 we can write the more sensible (see Test::More for a full
205 explanation of \fBis()\fR and other testing functions).
206 .PP
207 .Vb 2
208 \& is( "1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000),
209 \& "U* produces Unicode" );
210 .Ve
211 .PP
212 Now we\*(Aqll test that we got that space\-at\-the\-beginning business right:
213 .PP
214 .Vb 2
215 \& is( "1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000),
216 \& " with spaces at the beginning" );
217 .Ve
218 .PP
219 And finally we\*(Aqll test that we don\*(Aqt make Unicode strings if \f(CW\*(C`U\*(C'\fR is
220 \&\fBnot\fR the first active format:
221 .PP
222 .Vb 2
223 \& isnt( v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000),
224 \& "U* not first isn\*(Aqt Unicode" );
225 .Ve
226 .PP
227 Mustn\*(Aqt forget to change the number of tests which appears at the top,
228 or else the automated tester will get confused. This will either look
229 like this:
230 .PP
231 .Vb 1
232 \& print "1..156\en";
233 .Ve
234 .PP
235 or this:
236 .PP
237 .Vb 1
238 \& plan( tests => 156 );
239 .Ve
240 .PP
241 We now compile up Perl, and run it through the test suite. Our new
242 tests pass, hooray!
243 .SS "Documenting the patch"
244 .IX Subsection "Documenting the patch"
245 Finally, the documentation. The job is never done until the paperwork
246 is over, so let\*(Aqs describe the change we\*(Aqve just made. The relevant
247 place is \fIpod/perlfunc.pod\fR; again, we make a copy, and then we\*(Aqll
248 insert this text in the description of \f(CW\*(C`pack\*(C'\fR:
249 .PP
250 .Vb 1
251 \& =item *
252 \&
253 \& If the pattern begins with a C<U>, the resulting string will be treated
254 \& as UTF\-8\-encoded Unicode. You can force UTF\-8 encoding on in a string
255 \& with an initial C<U0>, and the bytes that follow will be interpreted as
256 \& Unicode characters. If you don\*(Aqt want this to happen, you can begin
257 \& your pattern with C<C0> (or anything else) to force Perl not to UTF\-8
258 \& encode your string, and then follow this with a C<U*> somewhere in your
259 \& pattern.
260 .Ve
261 .SS Submit
262 .IX Subsection "Submit"
263 See perlhack for details on how to submit this patch.
264 .SH AUTHOR
265 .IX Header "AUTHOR"
266 This document was originally written by Nathan Torkington, and is
267 maintained by the perl5\-porters mailing list.