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