4 Consistent formatting of this file is achieved with:
5 perl ./Porting/podtidy pod/perlhacktut.pod
9 perlhacktut - Walk through the creation of a simple C code patch
13 This document takes you through a simple patch example.
15 If you haven't read L<perlhack> yet, go do that first! You might also
16 want to read through L<perlsource> too.
18 Once you're done here, check out L<perlhacktips> next.
20 =head1 EXAMPLE OF A SIMPLE PATCH
22 Let's take a simple patch from start to finish.
24 Here's something Larry suggested: if a C<U> is the first active format
25 during a C<pack>, (for example, C<pack "U3C8", @stuff>) then the
26 resulting string should be treated as UTF-8 encoded.
28 If you are working with a git clone of the Perl repository, you will
29 want to create a branch for your changes. This will make creating a
30 proper patch much simpler. See the L<perlgit> for details on how to do
33 =head2 Writing the patch
35 How do we prepare to fix this up? First we locate the code in question
36 - the C<pack> happens at runtime, so it's going to be in one of the
37 F<pp> files. Sure enough, C<pp_pack> is in F<pp.c>. Since we're going
38 to be altering this file, let's copy it to F<pp.c~>.
40 [Well, it was in F<pp.c> when this tutorial was written. It has now
41 been split off with C<pp_unpack> to its own file, F<pp_pack.c>]
43 Now let's look over C<pp_pack>: we take a pattern into C<pat>, and then
44 loop over the pattern, taking each format character in turn into
45 C<datum_type>. Then for each possible format character, we swallow up
46 the other arguments in the pattern (a field width, an asterisk, and so
47 on) and convert the next chunk input into the specified format, adding
48 it onto the output SV C<cat>.
50 How do we know if the C<U> is the first format in the C<pat>? Well, if
51 we have a pointer to the start of C<pat> then, if we see a C<U> we can
52 test whether we're still at the start of the string. So, here's where
56 char *pat = SvPVx(*++MARK, fromlen);
57 char *patend = pat + fromlen;
62 We'll have another string pointer in there:
65 char *pat = SvPVx(*++MARK, fromlen);
66 char *patend = pat + fromlen;
72 And just before we start the loop, we'll set C<patcopy> to be the start
77 sv_setpvn(cat, "", 0);
79 while (pat < patend) {
81 Now if we see a C<U> which was at the start of the string, we turn on
82 the C<UTF8> flag for the output SV, C<cat>:
84 + if (datumtype == 'U' && pat==patcopy+1)
86 if (datumtype == '#') {
87 while (pat < patend && *pat != '\n')
90 Remember that it has to be C<patcopy+1> because the first character of
91 the string is the C<U> which has been swallowed into C<datumtype!>
93 Oops, we forgot one thing: what if there are spaces at the start of the
94 pattern? C<pack(" U*", @stuff)> will have C<U> as the first active
95 character, even though it's not the first thing in the pattern. In this
96 case, we have to advance C<patcopy> along with C<pat> when we see
99 if (isSPACE(datumtype))
104 if (isSPACE(datumtype)) {
109 OK. That's the C part done. Now we must do two additional things before
110 this patch is ready to go: we've changed the behaviour of Perl, and so
111 we must document that change. We must also provide some more regression
112 tests to make sure our patch works and doesn't create a bug somewhere
115 =head2 Testing the patch
117 The regression tests for each operator live in F<t/op/>, and so we make
118 a copy of F<t/op/pack.t> to F<t/op/pack.t~>. Now we can add our tests
119 to the end. First, we'll test that the C<U> does indeed create Unicode
122 t/op/pack.t has a sensible ok() function, but if it didn't we could use
123 the one from t/test.pl.
126 plan( tests => 159 );
130 print 'not ' unless "1.20.300.4000" eq sprintf "%vd",
131 pack("U*",1,20,300,4000);
132 print "ok $test\n"; $test++;
134 we can write the more sensible (see L<Test::More> for a full
135 explanation of is() and other testing functions).
137 is( "1.20.300.4000", sprintf "%vd", pack("U*",1,20,300,4000),
138 "U* produces Unicode" );
140 Now we'll test that we got that space-at-the-beginning business right:
142 is( "1.20.300.4000", sprintf "%vd", pack(" U*",1,20,300,4000),
143 " with spaces at the beginning" );
145 And finally we'll test that we don't make Unicode strings if C<U> is
146 B<not> the first active format:
148 isnt( v1.20.300.4000, sprintf "%vd", pack("C0U*",1,20,300,4000),
149 "U* not first isn't Unicode" );
151 Mustn't forget to change the number of tests which appears at the top,
152 or else the automated tester will get confused. This will either look
159 plan( tests => 156 );
161 We now compile up Perl, and run it through the test suite. Our new
164 =head2 Documenting the patch
166 Finally, the documentation. The job is never done until the paperwork
167 is over, so let's describe the change we've just made. The relevant
168 place is F<pod/perlfunc.pod>; again, we make a copy, and then we'll
169 insert this text in the description of C<pack>:
173 If the pattern begins with a C<U>, the resulting string will be treated
174 as UTF-8-encoded Unicode. You can force UTF-8 encoding on in a string
175 with an initial C<U0>, and the bytes that follow will be interpreted as
176 Unicode characters. If you don't want this to happen, you can begin
177 your pattern with C<C0> (or anything else) to force Perl not to UTF-8
178 encode your string, and then follow this with a C<U*> somewhere in your
183 See L<perlhack> for details on how to submit this patch.
187 This document was originally written by Nathan Torkington, and is
188 maintained by the perl5-porters mailing list.