1PERLHACKTUT(1)         Perl Programmers Reference Guide         PERLHACKTUT(1)
2
3
4

NAME

6       perlhacktut - Walk through the creation of a simple C code patch
7

DESCRIPTION

9       This document takes you through a simple patch example.
10
11       If you haven't read perlhack yet, go do that first! You might also want
12       to read through perlsource too.
13
14       Once you're done here, check out perlhacktips next.
15

EXAMPLE OF A SIMPLE PATCH

17       Let's take a simple patch from start to finish.
18
19       Here's something Larry suggested: if a "U" is the first active format
20       during a "pack", (for example, "pack "U3C8", @stuff") then the
21       resulting string should be treated as UTF-8 encoded.
22
23       If you are working with a git clone of the Perl repository, you will
24       want to create a branch for your changes. This will make creating a
25       proper patch much simpler. See the perlgit for details on how to do
26       this.
27
28   Writing the patch
29       How do we prepare to fix this up? First we locate the code in question
30       - the "pack" happens at runtime, so it's going to be in one of the pp
31       files. Sure enough, "pp_pack" is in pp.c. Since we're going to be
32       altering this file, let's copy it to pp.c~.
33
34       [Well, it was in pp.c when this tutorial was written. It has now been
35       split off with "pp_unpack" to its own file, pp_pack.c]
36
37       Now let's look over "pp_pack": we take a pattern into "pat", and then
38       loop over the pattern, taking each format character in turn into
39       "datum_type". Then for each possible format character, we swallow up
40       the other arguments in the pattern (a field width, an asterisk, and so
41       on) and convert the next chunk input into the specified format, adding
42       it onto the output SV "cat".
43
44       How do we know if the "U" is the first format in the "pat"? Well, if we
45       have a pointer to the start of "pat" then, if we see a "U" we can test
46       whether we're still at the start of the string. So, here's where "pat"
47       is set up:
48
49           STRLEN fromlen;
50           char *pat = SvPVx(*++MARK, fromlen);
51           char *patend = pat + fromlen;
52           I32 len;
53           I32 datumtype;
54           SV *fromstr;
55
56       We'll have another string pointer in there:
57
58           STRLEN fromlen;
59           char *pat = SvPVx(*++MARK, fromlen);
60           char *patend = pat + fromlen;
61        +  char *patcopy;
62           I32 len;
63           I32 datumtype;
64           SV *fromstr;
65
66       And just before we start the loop, we'll set "patcopy" to be the start
67       of "pat":
68
69           items = SP - MARK;
70           MARK++;
71           SvPVCLEAR(cat);
72        +  patcopy = pat;
73           while (pat < patend) {
74
75       Now if we see a "U" which was at the start of the string, we turn on
76       the "UTF8" flag for the output SV, "cat":
77
78        +  if (datumtype == 'U' && pat==patcopy+1)
79        +      SvUTF8_on(cat);
80           if (datumtype == '#') {
81               while (pat < patend && *pat != '\n')
82                   pat++;
83
84       Remember that it has to be "patcopy+1" because the first character of
85       the string is the "U" which has been swallowed into "datumtype!"
86
87       Oops, we forgot one thing: what if there are spaces at the start of the
88       pattern? "pack("  U*", @stuff)" will have "U" as the first active
89       character, even though it's not the first thing in the pattern. In this
90       case, we have to advance "patcopy" along with "pat" when we see spaces:
91
92           if (isSPACE(datumtype))
93               continue;
94
95       needs to become
96
97           if (isSPACE(datumtype)) {
98               patcopy++;
99               continue;
100           }
101
102       OK. That's the C part done. Now we must do two additional things before
103       this patch is ready to go: we've changed the behaviour of Perl, and so
104       we must document that change. We must also provide some more regression
105       tests to make sure our patch works and doesn't create a bug somewhere
106       else along the line.
107
108   Testing the patch
109       The regression tests for each operator live in t/op/, and so we make a
110       copy of t/op/pack.t to t/op/pack.t~. Now we can add our tests to the
111       end. First, we'll test that the "U" does indeed create Unicode strings.
112
113       t/op/pack.t has a sensible ok() function, but if it didn't we could use
114       the one from t/test.pl.
115
116        require './test.pl';
117        plan( tests => 159 );
118
119       so instead of this:
120
121        print 'not ' unless "1.20.300.4000" eq sprintf "%vd",
122                                                      pack("U*",1,20,300,4000);
123        print "ok $test\n"; $test++;
124
125       we can write the more sensible (see Test::More for a full explanation
126       of is() and other testing functions).
127
128        is( "1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000),
129                                              "U* produces Unicode" );
130
131       Now we'll test that we got that space-at-the-beginning business right:
132
133        is( "1.20.300.4000", sprintf "%vd", pack("  U*",1,20,300,4000),
134                                            "  with spaces at the beginning" );
135
136       And finally we'll test that we don't make Unicode strings if "U" is not
137       the first active format:
138
139        isnt( v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000),
140                                              "U* not first isn't Unicode" );
141
142       Mustn't forget to change the number of tests which appears at the top,
143       or else the automated tester will get confused. This will either look
144       like this:
145
146        print "1..156\n";
147
148       or this:
149
150        plan( tests => 156 );
151
152       We now compile up Perl, and run it through the test suite. Our new
153       tests pass, hooray!
154
155   Documenting the patch
156       Finally, the documentation. The job is never done until the paperwork
157       is over, so let's describe the change we've just made. The relevant
158       place is pod/perlfunc.pod; again, we make a copy, and then we'll insert
159       this text in the description of "pack":
160
161        =item *
162
163        If the pattern begins with a C<U>, the resulting string will be treated
164        as UTF-8-encoded Unicode. You can force UTF-8 encoding on in a string
165        with an initial C<U0>, and the bytes that follow will be interpreted as
166        Unicode characters. If you don't want this to happen, you can begin
167        your pattern with C<C0> (or anything else) to force Perl not to UTF-8
168        encode your string, and then follow this with a C<U*> somewhere in your
169        pattern.
170
171   Submit
172       See perlhack for details on how to submit this patch.
173

AUTHOR

175       This document was originally written by Nathan Torkington, and is
176       maintained by the perl5-porters mailing list.
177
178
179
180perl v5.30.2                      2020-03-27                    PERLHACKTUT(1)
Impressum