Commit | Line | Data |
4633a7c4 |
1 | =head1 NAME |
2 | |
3 | perlXStut - Tutorial for XSUB's |
4 | |
5 | =head1 DESCRIPTION |
6 | |
7 | This tutorial will educate the reader on the steps involved in creating |
c07a80fd |
8 | a Perl extension. The reader is assumed to have access to L<perlguts> and |
4633a7c4 |
9 | L<perlxs>. |
10 | |
11 | This tutorial starts with very simple examples and becomes more complex, |
c07a80fd |
12 | with each new example adding new features. Certain concepts may not be |
13 | completely explained until later in the tutorial in order to slowly ease |
14 | the reader into building extensions. |
4633a7c4 |
15 | |
c07a80fd |
16 | =head2 VERSION CAVEAT |
4633a7c4 |
17 | |
c07a80fd |
18 | This tutorial tries hard to keep up with the latest development versions |
19 | of Perl. This often means that it is sometimes in advance of the latest |
20 | released version of Perl, and that certain features described here might |
21 | not work on earlier versions. This section will keep track of when various |
22 | features were added to Perl 5. |
23 | |
24 | =over 4 |
25 | |
26 | =item * |
27 | |
d9d2a7fb |
28 | In versions of 5.002 prior to the gamma version, the test script in Example |
29 | 1 will not function properly. You need to change the "use lib" line to |
30 | read: |
31 | |
32 | use lib './blib'; |
33 | |
34 | =item * |
35 | |
791fa977 |
36 | In versions of 5.002 prior to version beta 3, then the line in the .xs file |
37 | about "PROTOTYPES: DISABLE" will cause a compiler error. Simply remove that |
38 | line from the file. |
39 | |
40 | =item * |
41 | |
42 | In versions of 5.002 prior to version 5.002b1h, the test.pl file was not |
43 | automatically created by h2xs. This means that you cannot say "make test" |
c07a80fd |
44 | to run the test script. You will need to add the following line before the |
45 | "use extension" statement: |
46 | |
47 | use lib './blib'; |
48 | |
49 | =item * |
50 | |
51 | In versions 5.000 and 5.001, instead of using the above line, you will need |
52 | to use the following line: |
53 | |
54 | BEGIN { unshift(@INC, "./blib") } |
55 | |
56 | =item * |
57 | |
58 | This document assumes that the executable named "perl" is Perl version 5. |
59 | Some systems may have installed Perl version 5 as "perl5". |
60 | |
61 | =back |
62 | |
63 | =head2 DYNAMIC VERSUS STATIC |
64 | |
65 | It is commonly thought that if a system does not have the capability to |
66 | dynamically load a library, you cannot build XSUB's. This is incorrect. |
67 | You I<can> build them, but you must link the XSUB's subroutines with the |
68 | rest of Perl, creating a new executable. This situation is similar to |
69 | Perl 4. |
70 | |
71 | This tutorial can still be used on such a system. The XSUB build mechanism |
72 | will check the system and build a dynamically-loadable library if possible, |
73 | or else a static library and then, optionally, a new statically-linked |
74 | executable with that static library linked in. |
75 | |
76 | Should you wish to build a statically-linked executable on a system which |
77 | can dynamically load libraries, you may, in all the following examples, |
78 | where the command "make" with no arguments is executed, run the command |
79 | "make perl" instead. |
80 | |
81 | If you have generated such a statically-linked executable by choice, then |
82 | instead of saying "make test", you should say "make test_static". On systems |
83 | that cannot build dynamically-loadable libraries at all, simply saying "make |
84 | test" is sufficient. |
85 | |
86 | =head2 EXAMPLE 1 |
4633a7c4 |
87 | |
88 | Our first extension will be very simple. When we call the routine in the |
c07a80fd |
89 | extension, it will print out a well-known message and return. |
4633a7c4 |
90 | |
84dc3c4d |
91 | Run C<h2xs -A -n Mytest>. This creates a directory named Mytest, possibly under |
c07a80fd |
92 | ext/ if that directory exists in the current working directory. Several files |
791fa977 |
93 | will be created in the Mytest dir, including MANIFEST, Makefile.PL, Mytest.pm, |
94 | Mytest.xs, test.pl, and Changes. |
4633a7c4 |
95 | |
c07a80fd |
96 | The MANIFEST file contains the names of all the files created. |
4633a7c4 |
97 | |
98 | The file Makefile.PL should look something like this: |
99 | |
100 | use ExtUtils::MakeMaker; |
101 | # See lib/ExtUtils/MakeMaker.pm for details of how to influence |
102 | # the contents of the Makefile that is written. |
103 | WriteMakefile( |
791fa977 |
104 | 'NAME' => 'Mytest', |
105 | 'VERSION_FROM' => 'Mytest.pm', # finds $VERSION |
4633a7c4 |
106 | 'LIBS' => [''], # e.g., '-lm' |
107 | 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' |
108 | 'INC' => '', # e.g., '-I/usr/include/other' |
109 | ); |
110 | |
791fa977 |
111 | The file Mytest.pm should start with something like this: |
c07a80fd |
112 | |
791fa977 |
113 | package Mytest; |
4633a7c4 |
114 | |
4633a7c4 |
115 | require Exporter; |
116 | require DynaLoader; |
c07a80fd |
117 | |
4633a7c4 |
118 | @ISA = qw(Exporter DynaLoader); |
119 | # Items to export into callers namespace by default. Note: do not export |
120 | # names by default without a very good reason. Use EXPORT_OK instead. |
121 | # Do not simply export all your public functions/methods/constants. |
122 | @EXPORT = qw( |
c07a80fd |
123 | |
4633a7c4 |
124 | ); |
c07a80fd |
125 | $VERSION = '0.01'; |
126 | |
791fa977 |
127 | bootstrap Mytest $VERSION; |
c07a80fd |
128 | |
4633a7c4 |
129 | # Preloaded methods go here. |
c07a80fd |
130 | |
4633a7c4 |
131 | # Autoload methods go after __END__, and are processed by the autosplit program. |
c07a80fd |
132 | |
4633a7c4 |
133 | 1; |
134 | __END__ |
c07a80fd |
135 | # Below is the stub of documentation for your module. You better edit it! |
4633a7c4 |
136 | |
791fa977 |
137 | And the Mytest.xs file should look something like this: |
4633a7c4 |
138 | |
c07a80fd |
139 | #ifdef __cplusplus |
140 | extern "C" { |
141 | #endif |
4633a7c4 |
142 | #include "EXTERN.h" |
143 | #include "perl.h" |
144 | #include "XSUB.h" |
c07a80fd |
145 | #ifdef __cplusplus |
146 | } |
147 | #endif |
4633a7c4 |
148 | |
791fa977 |
149 | PROTOTYPES: DISABLE |
150 | |
151 | MODULE = Mytest PACKAGE = Mytest |
4633a7c4 |
152 | |
153 | Let's edit the .xs file by adding this to the end of the file: |
154 | |
155 | void |
156 | hello() |
4633a7c4 |
157 | CODE: |
158 | printf("Hello, world!\n"); |
159 | |
c07a80fd |
160 | Now we'll run "perl Makefile.PL". This will create a real Makefile, |
d9d2a7fb |
161 | which make needs. Its output looks something like: |
4633a7c4 |
162 | |
163 | % perl Makefile.PL |
164 | Checking if your kit is complete... |
165 | Looks good |
791fa977 |
166 | Writing Makefile for Mytest |
4633a7c4 |
167 | % |
168 | |
c07a80fd |
169 | Now, running make will produce output that looks something like this |
170 | (some long lines shortened for clarity): |
4633a7c4 |
171 | |
172 | % make |
791fa977 |
173 | umask 0 && cp Mytest.pm ./blib/Mytest.pm |
174 | perl xsubpp -typemap typemap Mytest.xs >Mytest.tc && mv Mytest.tc Mytest.c |
175 | cc -c Mytest.c |
176 | Running Mkbootstrap for Mytest () |
177 | chmod 644 Mytest.bs |
178 | LD_RUN_PATH="" ld -o ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl -b Mytest.o |
179 | chmod 755 ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl |
180 | cp Mytest.bs ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs |
181 | chmod 644 ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs |
c07a80fd |
182 | |
183 | Now, although there is already a test.pl template ready for us, for this |
184 | example only, we'll create a special test script. Create a file called hello |
185 | that looks like this: |
186 | |
c07a80fd |
187 | #! /opt/perl5/bin/perl |
4633a7c4 |
188 | |
d9d2a7fb |
189 | use ExtUtils::testlib; |
4633a7c4 |
190 | |
791fa977 |
191 | use Mytest; |
4633a7c4 |
192 | |
791fa977 |
193 | Mytest::hello(); |
4633a7c4 |
194 | |
195 | Now we run the script and we should see the following output: |
196 | |
c07a80fd |
197 | % perl hello |
4633a7c4 |
198 | Hello, world! |
199 | % |
200 | |
c07a80fd |
201 | =head2 EXAMPLE 2 |
4633a7c4 |
202 | |
c07a80fd |
203 | Now let's add to our extension a subroutine that will take a single argument |
204 | and return 0 if the argument is even, 1 if the argument is odd. |
4633a7c4 |
205 | |
791fa977 |
206 | Add the following to the end of Mytest.xs: |
4633a7c4 |
207 | |
208 | int |
209 | is_even(input) |
210 | int input |
4633a7c4 |
211 | CODE: |
c07a80fd |
212 | RETVAL = (input % 2 == 0); |
4633a7c4 |
213 | OUTPUT: |
214 | RETVAL |
215 | |
791fa977 |
216 | There does not need to be white space at the start of the "int input" line, |
217 | but it is useful for improving readability. The semi-colon at the end of |
218 | that line is also optional. |
4633a7c4 |
219 | |
c07a80fd |
220 | Any white space may be between the "int" and "input". It is also okay for |
221 | the four lines starting at the "CODE:" line to not be indented. However, |
222 | for readability purposes, it is suggested that you indent them 8 spaces |
223 | (or one normal tab stop). |
4633a7c4 |
224 | |
c07a80fd |
225 | Now re-run make to rebuild our new shared library. |
4633a7c4 |
226 | |
c07a80fd |
227 | Now perform the same steps as before, generating a Makefile from the |
228 | Makefile.PL file, and running make. |
4633a7c4 |
229 | |
c07a80fd |
230 | In order to test that our extension works, we now need to look at the |
231 | file test.pl. This file is set up to imitate the same kind of testing |
232 | structure that Perl itself has. Within the test script, you perform a |
233 | number of tests to confirm the behavior of the extension, printing "ok" |
d9d2a7fb |
234 | when the test is correct, "not ok" when it is not. Change the print |
235 | statement in the BEGIN block to print "1..4", and add the following code |
236 | to the end of the file: |
c07a80fd |
237 | |
791fa977 |
238 | print &Mytest::is_even(0) == 1 ? "ok 2" : "not ok 2", "\n"; |
239 | print &Mytest::is_even(1) == 0 ? "ok 3" : "not ok 3", "\n"; |
240 | print &Mytest::is_even(2) == 1 ? "ok 4" : "not ok 4", "\n"; |
c07a80fd |
241 | |
242 | We will be calling the test script through the command "make test". You |
243 | should see output that looks something like this: |
244 | |
245 | % make test |
246 | PERL_DL_NONLAZY=1 /opt/perl5.002b2/bin/perl (lots of -I arguments) test.pl |
247 | 1..4 |
248 | ok 1 |
249 | ok 2 |
250 | ok 3 |
251 | ok 4 |
4633a7c4 |
252 | % |
253 | |
c07a80fd |
254 | =head2 WHAT HAS GONE ON? |
4633a7c4 |
255 | |
256 | The program h2xs is the starting point for creating extensions. In later |
c07a80fd |
257 | examples we'll see how we can use h2xs to read header files and generate |
4633a7c4 |
258 | templates to connect to C routines. |
259 | |
260 | h2xs creates a number of files in the extension directory. The file |
261 | Makefile.PL is a perl script which will generate a true Makefile to build |
262 | the extension. We'll take a closer look at it later. |
263 | |
264 | The files <extension>.pm and <extension>.xs contain the meat of the extension. |
265 | The .xs file holds the C routines that make up the extension. The .pm file |
c07a80fd |
266 | contains routines that tell Perl how to load your extension. |
4633a7c4 |
267 | |
c07a80fd |
268 | Generating and invoking the Makefile created a directory blib (which stands |
269 | for "build library") in the current working directory. This directory will |
270 | contain the shared library that we will build. Once we have tested it, we |
271 | can install it into its final location. |
272 | |
273 | Invoking the test script via "make test" did something very important. It |
84dc3c4d |
274 | invoked perl with all those C<-I> arguments so that it could find the various |
c07a80fd |
275 | files that are part of the extension. |
276 | |
277 | It is I<very> important that while you are still testing extensions that |
278 | you use "make test". If you try to run the test script all by itself, you |
279 | will get a fatal error. |
280 | |
281 | Another reason it is important to use "make test" to run your test script |
282 | is that if you are testing an upgrade to an already-existing version, using |
283 | "make test" insures that you use your new extension, not the already-existing |
284 | version. |
4633a7c4 |
285 | |
c07a80fd |
286 | When Perl sees a C<use extension;>, it searches for a file with the same name |
287 | as the use'd extension that has a .pm suffix. If that file cannot be found, |
288 | Perl dies with a fatal error. The default search path is contained in the |
289 | @INC array. |
4633a7c4 |
290 | |
791fa977 |
291 | In our case, Mytest.pm tells perl that it will need the Exporter and Dynamic |
c07a80fd |
292 | Loader extensions. It then sets the @ISA and @EXPORT arrays and the $VERSION |
293 | scalar; finally it tells perl to bootstrap the module. Perl will call its |
294 | dynamic loader routine (if there is one) and load the shared library. |
4633a7c4 |
295 | |
c07a80fd |
296 | The two arrays that are set in the .pm file are very important. The @ISA |
297 | array contains a list of other packages in which to search for methods (or |
298 | subroutines) that do not exist in the current package. The @EXPORT array |
299 | tells Perl which of the extension's routines should be placed into the |
300 | calling package's namespace. |
4633a7c4 |
301 | |
c07a80fd |
302 | It's important to select what to export carefully. Do NOT export method names |
303 | and do NOT export anything else I<by default> without a good reason. |
4633a7c4 |
304 | |
c07a80fd |
305 | As a general rule, if the module is trying to be object-oriented then don't |
306 | export anything. If it's just a collection of functions then you can export |
307 | any of the functions via another array, called @EXPORT_OK. |
4633a7c4 |
308 | |
c07a80fd |
309 | See L<perlmod> for more information. |
4633a7c4 |
310 | |
c07a80fd |
311 | The $VERSION variable is used to ensure that the .pm file and the shared |
791fa977 |
312 | library are "in sync" with each other. Any time you make changes to |
313 | the .pm or .xs files, you should increment the value of this variable. |
314 | |
315 | =head2 WRITING GOOD TEST SCRIPTS |
316 | |
317 | The importance of writing good test scripts cannot be overemphasized. You |
318 | should closely follow the "ok/not ok" style that Perl itself uses, so that |
319 | it is very easy and unambiguous to determine the outcome of each test case. |
320 | When you find and fix a bug, make sure you add a test case for it. |
321 | |
322 | By running "make test", you ensure that your test.pl script runs and uses |
323 | the correct version of your extension. If you have many test cases, you |
324 | might want to copy Perl's test style. Create a directory named "t", and |
325 | ensure all your test files end with the suffix ".t". The Makefile will |
326 | properly run all these test files. |
327 | |
4633a7c4 |
328 | |
c07a80fd |
329 | =head2 EXAMPLE 3 |
4633a7c4 |
330 | |
331 | Our third extension will take one argument as its input, round off that |
c07a80fd |
332 | value, and set the I<argument> to the rounded value. |
4633a7c4 |
333 | |
791fa977 |
334 | Add the following to the end of Mytest.xs: |
4633a7c4 |
335 | |
336 | void |
337 | round(arg) |
338 | double arg |
4633a7c4 |
339 | CODE: |
340 | if (arg > 0.0) { |
341 | arg = floor(arg + 0.5); |
342 | } else if (arg < 0.0) { |
343 | arg = ceil(arg - 0.5); |
344 | } else { |
345 | arg = 0.0; |
346 | } |
347 | OUTPUT: |
348 | arg |
349 | |
c07a80fd |
350 | Edit the Makefile.PL file so that the corresponding line looks like this: |
4633a7c4 |
351 | |
352 | 'LIBS' => ['-lm'], # e.g., '-lm' |
353 | |
c07a80fd |
354 | Generate the Makefile and run make. Change the BEGIN block to print out |
355 | "1..9" and add the following to test.pl: |
4633a7c4 |
356 | |
791fa977 |
357 | $i = -1.5; &Mytest::round($i); print $i == -2.0 ? "ok 5" : "not ok 5", "\n"; |
358 | $i = -1.1; &Mytest::round($i); print $i == -1.0 ? "ok 6" : "not ok 6", "\n"; |
359 | $i = 0.0; &Mytest::round($i); print $i == 0.0 ? "ok 7" : "not ok 7", "\n"; |
360 | $i = 0.5; &Mytest::round($i); print $i == 1.0 ? "ok 8" : "not ok 8", "\n"; |
361 | $i = 1.2; &Mytest::round($i); print $i == 1.0 ? "ok 9" : "not ok 9", "\n"; |
c07a80fd |
362 | |
363 | Running "make test" should now print out that all nine tests are okay. |
4633a7c4 |
364 | |
c07a80fd |
365 | You might be wondering if you can round a constant. To see what happens, add |
366 | the following line to test.pl temporarily: |
4633a7c4 |
367 | |
791fa977 |
368 | &Mytest::round(3); |
4633a7c4 |
369 | |
c07a80fd |
370 | Run "make test" and notice that Perl dies with a fatal error. Perl won't let |
371 | you change the value of constants! |
4633a7c4 |
372 | |
c07a80fd |
373 | =head2 WHAT'S NEW HERE? |
4633a7c4 |
374 | |
375 | Two things are new here. First, we've made some changes to Makefile.PL. |
d9d2a7fb |
376 | In this case, we've specified an extra library to link in, the math library |
377 | libm. We'll talk later about how to write XSUBs that can call every routine |
378 | in a library. |
4633a7c4 |
379 | |
380 | Second, the value of the function is being passed back not as the function's |
381 | return value, but through the same variable that was passed into the function. |
382 | |
c07a80fd |
383 | =head2 INPUT AND OUTPUT PARAMETERS |
4633a7c4 |
384 | |
385 | You specify the parameters that will be passed into the XSUB just after you |
791fa977 |
386 | declare the function return value and name. Each parameter line starts with |
387 | optional white space, and may have an optional terminating semicolon. |
4633a7c4 |
388 | |
389 | The list of output parameters occurs after the OUTPUT: directive. The use |
390 | of RETVAL tells Perl that you wish to send this value back as the return |
c07a80fd |
391 | value of the XSUB function. In Example 3, the value we wanted returned was |
392 | contained in the same variable we passed in, so we listed it (and not RETVAL) |
393 | in the OUTPUT: section. |
4633a7c4 |
394 | |
c07a80fd |
395 | =head2 THE XSUBPP COMPILER |
4633a7c4 |
396 | |
397 | The compiler xsubpp takes the XS code in the .xs file and converts it into |
398 | C code, placing it in a file whose suffix is .c. The C code created makes |
399 | heavy use of the C functions within Perl. |
400 | |
c07a80fd |
401 | =head2 THE TYPEMAP FILE |
4633a7c4 |
402 | |
403 | The xsubpp compiler uses rules to convert from Perl's data types (scalar, |
404 | array, etc.) to C's data types (int, char *, etc.). These rules are stored |
405 | in the typemap file ($PERLLIB/ExtUtils/typemap). This file is split into |
406 | three parts. |
407 | |
408 | The first part attempts to map various C data types to a coded flag, which |
409 | has some correspondence with the various Perl types. The second part contains |
410 | C code which xsubpp uses for input parameters. The third part contains C |
411 | code which xsubpp uses for output parameters. We'll talk more about the |
412 | C code later. |
413 | |
c07a80fd |
414 | Let's now take a look at a portion of the .c file created for our extension. |
4633a7c4 |
415 | |
791fa977 |
416 | XS(XS_Mytest_round) |
4633a7c4 |
417 | { |
418 | dXSARGS; |
c07a80fd |
419 | if (items != 1) |
791fa977 |
420 | croak("Usage: Mytest::round(arg)"); |
4633a7c4 |
421 | { |
c07a80fd |
422 | double arg = (double)SvNV(ST(0)); /* XXXXX */ |
4633a7c4 |
423 | if (arg > 0.0) { |
424 | arg = floor(arg + 0.5); |
425 | } else if (arg < 0.0) { |
426 | arg = ceil(arg - 0.5); |
c07a80fd |
427 | } else { |
428 | arg = 0.0; |
4633a7c4 |
429 | } |
c07a80fd |
430 | sv_setnv(ST(0), (double)arg); /* XXXXX */ |
4633a7c4 |
431 | } |
432 | XSRETURN(1); |
433 | } |
4633a7c4 |
434 | |
435 | Notice the two lines marked with "XXXXX". If you check the first section of |
436 | the typemap file, you'll see that doubles are of type T_DOUBLE. In the |
437 | INPUT section, an argument that is T_DOUBLE is assigned to the variable |
438 | arg by calling the routine SvNV on something, then casting it to double, |
439 | then assigned to the variable arg. Similarly, in the OUTPUT section, |
440 | once arg has its final value, it is passed to the sv_setnv function to |
441 | be passed back to the calling subroutine. These two functions are explained |
c07a80fd |
442 | in L<perlguts>; we'll talk more later about what that "ST(0)" means in the |
4633a7c4 |
443 | section on the argument stack. |
444 | |
c07a80fd |
445 | =head2 WARNING |
4633a7c4 |
446 | |
c07a80fd |
447 | In general, it's not a good idea to write extensions that modify their input |
4633a7c4 |
448 | parameters, as in Example 3. However, in order to better accomodate calling |
449 | pre-existing C routines, which often do modify their input parameters, |
d9d2a7fb |
450 | this behavior is tolerated. The next example will show how to do this. |
791fa977 |
451 | |
452 | =head2 EXAMPLE 4 |
453 | |
454 | In this example, we'll now begin to write XSUB's that will interact with |
455 | pre-defined C libraries. To begin with, we will build a small library of |
456 | our own, then let h2xs write our .pm and .xs files for us. |
457 | |
458 | Create a new directory called Mytest2 at the same level as the directory |
459 | Mytest. In the Mytest2 directory, create another directory called mylib, |
460 | and cd into that directory. |
461 | |
462 | Here we'll create some files that will generate a test library. These will |
463 | include a C source file and a header file. We'll also create a Makefile.PL |
464 | in this directory. Then we'll make sure that running make at the Mytest2 |
465 | level will automatically run this Makefile.PL file and the resulting Makefile. |
466 | |
467 | In the testlib directory, create a file mylib.h that looks like this: |
468 | |
469 | #define TESTVAL 4 |
470 | |
471 | extern double foo(int, long, const char*); |
472 | |
473 | Also create a file mylib.c that looks like this: |
474 | |
475 | #include <stdlib.h> |
476 | #include "./mylib.h" |
477 | |
478 | double |
479 | foo(a, b, c) |
480 | int a; |
481 | long b; |
482 | const char * c; |
483 | { |
484 | return (a + b + atof(c) + TESTVAL); |
485 | } |
486 | |
487 | And finally create a file Makefile.PL that looks like this: |
488 | |
489 | use ExtUtils::MakeMaker; |
490 | $Verbose = 1; |
491 | WriteMakefile( |
492 | 'NAME' => 'Mytest2::mylib', |
493 | 'clean' => {'FILES' => 'libmylib.a'}, |
494 | ); |
495 | |
496 | |
497 | sub MY::postamble { |
498 | ' |
499 | all :: static |
500 | |
501 | static :: libmylib$(LIB_EXT) |
502 | |
503 | libmylib$(LIB_EXT): $(O_FILES) |
504 | $(AR) cr libmylib$(LIB_EXT) $(O_FILES) |
505 | $(RANLIB) libmylib$(LIB_EXT) |
506 | |
507 | '; |
508 | } |
509 | |
510 | We will now create the main top-level Mytest2 files. Change to the directory |
511 | above Mytest2 and run the following command: |
512 | |
d9d2a7fb |
513 | % h2xs -O -n Mytest2 ./Mytest2/mylib/mylib.h |
791fa977 |
514 | |
515 | This will print out a warning about overwriting Mytest2, but that's okay. |
516 | Our files are stored in Mytest2/mylib, and will be untouched. |
517 | |
518 | The normal Makefile.PL that h2xs generates doesn't know about the mylib |
519 | directory. We need to tell it that there is a subdirectory and that we |
520 | will be generating a library in it. Let's add the following key-value |
521 | pair to the WriteMakefile call: |
4633a7c4 |
522 | |
791fa977 |
523 | 'MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)', |
524 | |
525 | and a new replacement subroutine too: |
526 | |
527 | sub MY::postamble { |
528 | ' |
529 | $(MYEXTLIB): mylib/Makefile |
530 | cd mylib && $(MAKE) |
531 | '; |
532 | } |
533 | |
534 | (Note: Most makes will require that there be a tab character that indents |
535 | the line "cd mylib && $(MAKE)".) |
536 | |
537 | Let's also fix the MANIFEST file so that it accurately reflects the contents |
538 | of our extension. The single line that says "mylib" should be replaced by |
539 | the following three lines: |
540 | |
541 | mylib/Makefile.PL |
542 | mylib/mylib.c |
543 | mylib/mylib.h |
544 | |
545 | To keep our namespace nice and unpolluted, edit the .pm file and change |
d9d2a7fb |
546 | the lines setting @EXPORT to @EXPORT_OK (there are two: one in the line |
547 | beginning "use vars" and one setting the array itself). Finally, in the |
548 | .xs file, edit the #include line to read: |
791fa977 |
549 | |
550 | #include "mylib/mylib.h" |
551 | |
552 | And also add the following function definition to the end of the .xs file: |
553 | |
554 | double |
555 | foo(a,b,c) |
556 | int a |
557 | long b |
558 | const char * c |
559 | OUTPUT: |
560 | RETVAL |
561 | |
562 | Now we also need to create a typemap file because the default Perl doesn't |
563 | currently support the const char * type. Create a file called typemap and |
564 | place the following in it: |
565 | |
566 | const char * T_PV |
567 | |
568 | Now run perl on the top-level Makefile.PL. Notice that it also created a |
569 | Makefile in the mylib directory. Run make and see that it does cd into |
570 | the mylib directory and run make in there as well. |
571 | |
572 | Now edit the test.pl script and change the BEGIN block to print "1..4", |
573 | and add the following lines to the end of the script: |
574 | |
575 | print &Mytest2::foo(1, 2, "Hello, world!") == 7 ? "ok 2\n" : "not ok 2\n"; |
576 | print &Mytest2::foo(1, 2, "0.0") == 7 ? "ok 3\n" : "not ok 3\n"; |
577 | print abs(&Mytest2::foo(0, 0, "-3.4") - 0.6) <= 0.01 ? "ok 4\n" : "not ok 4\n"; |
578 | |
579 | (When dealing with floating-point comparisons, it is often useful to not check |
580 | for equality, but rather the difference being below a certain epsilon factor, |
581 | 0.01 in this case) |
582 | |
583 | Run "make test" and all should be well. |
584 | |
84dc3c4d |
585 | =head2 WHAT HAS HAPPENED HERE? |
791fa977 |
586 | |
587 | Unlike previous examples, we've now run h2xs on a real include file. This |
588 | has caused some extra goodies to appear in both the .pm and .xs files. |
589 | |
84dc3c4d |
590 | =over 4 |
591 | |
791fa977 |
592 | =item * |
593 | |
594 | In the .xs file, there's now a #include declaration with the full path to |
595 | the mylib.h header file. |
596 | |
597 | =item * |
598 | |
599 | There's now some new C code that's been added to the .xs file. The purpose |
600 | of the C<constant> routine is to make the values that are #define'd in the |
601 | header file available to the Perl script (in this case, by calling |
602 | C<&main::TESTVAL>). There's also some XS code to allow calls to the |
603 | C<constant> routine. |
604 | |
605 | =item * |
606 | |
607 | The .pm file has exported the name TESTVAL in the @EXPORT array. This |
608 | could lead to name clashes. A good rule of thumb is that if the #define |
609 | is only going to be used by the C routines themselves, and not by the user, |
610 | they should be removed from the @EXPORT array. Alternately, if you don't |
611 | mind using the "fully qualified name" of a variable, you could remove most |
612 | or all of the items in the @EXPORT array. |
613 | |
d9d2a7fb |
614 | =item * |
615 | |
616 | If our include file contained #include directives, these would not be |
617 | processed at all by h2xs. There is no good solution to this right now. |
618 | |
791fa977 |
619 | =back |
620 | |
621 | We've also told Perl about the library that we built in the mylib |
622 | subdirectory. That required only the addition of the MYEXTLIB variable |
623 | to the WriteMakefile call and the replacement of the postamble subroutine |
624 | to cd into the subdirectory and run make. The Makefile.PL for the |
625 | library is a bit more complicated, but not excessively so. Again we |
626 | replaced the postamble subroutine to insert our own code. This code |
627 | simply specified that the library to be created here was a static |
628 | archive (as opposed to a dynamically loadable library) and provided the |
629 | commands to build it. |
4633a7c4 |
630 | |
c07a80fd |
631 | =head2 SPECIFYING ARGUMENTS TO XSUBPP |
4633a7c4 |
632 | |
791fa977 |
633 | With the completion of Example 4, we now have an easy way to simulate some |
c07a80fd |
634 | real-life libraries whose interfaces may not be the cleanest in the world. |
635 | We shall now continue with a discussion of the arguments passed to the |
636 | xsubpp compiler. |
4633a7c4 |
637 | |
c07a80fd |
638 | When you specify arguments in the .xs file, you are really passing three |
639 | pieces of information for each one listed. The first piece is the order |
640 | of that argument relative to the others (first, second, etc). The second |
641 | is the type of argument, and consists of the type declaration of the |
642 | argument (e.g., int, char*, etc). The third piece is the exact way in |
643 | which the argument should be used in the call to the library function |
644 | from this XSUB. This would mean whether or not to place a "&" before |
645 | the argument or not, meaning the argument expects to be passed the address |
646 | of the specified data type. |
4633a7c4 |
647 | |
c07a80fd |
648 | There is a difference between the two arguments in this hypothetical function: |
4633a7c4 |
649 | |
4633a7c4 |
650 | int |
c07a80fd |
651 | foo(a,b) |
652 | char &a |
653 | char * b |
4633a7c4 |
654 | |
c07a80fd |
655 | The first argument to this function would be treated as a char and assigned |
656 | to the variable a, and its address would be passed into the function foo. |
657 | The second argument would be treated as a string pointer and assigned to the |
658 | variable b. The I<value> of b would be passed into the function foo. The |
659 | actual call to the function foo that xsubpp generates would look like this: |
4633a7c4 |
660 | |
c07a80fd |
661 | foo(&a, b); |
4633a7c4 |
662 | |
791fa977 |
663 | Xsubpp will identically parse the following function argument lists: |
664 | |
665 | char &a |
666 | char&a |
667 | char & a |
668 | |
669 | However, to help ease understanding, it is suggested that you place a "&" |
670 | next to the variable name and away from the variable type), and place a |
671 | "*" near the variable type, but away from the variable name (as in the |
672 | complete example above). By doing so, it is easy to understand exactly |
673 | what will be passed to the C function -- it will be whatever is in the |
674 | "last column". |
4633a7c4 |
675 | |
c07a80fd |
676 | You should take great pains to try to pass the function the type of variable |
677 | it wants, when possible. It will save you a lot of trouble in the long run. |
4633a7c4 |
678 | |
c07a80fd |
679 | =head2 THE ARGUMENT STACK |
4633a7c4 |
680 | |
c07a80fd |
681 | If we look at any of the C code generated by any of the examples except |
682 | example 1, you will notice a number of references to ST(n), where n is |
683 | usually 0. The "ST" is actually a macro that points to the n'th argument |
684 | on the argument stack. ST(0) is thus the first argument passed to the |
685 | XSUB, ST(1) is the second argument, and so on. |
4633a7c4 |
686 | |
c07a80fd |
687 | When you list the arguments to the XSUB in the .xs file, that tell xsubpp |
688 | which argument corresponds to which of the argument stack (i.e., the first |
689 | one listed is the first argument, and so on). You invite disaster if you |
690 | do not list them in the same order as the function expects them. |
4633a7c4 |
691 | |
c07a80fd |
692 | =head2 EXTENDING YOUR EXTENSION |
4633a7c4 |
693 | |
c07a80fd |
694 | Sometimes you might want to provide some extra methods or subroutines |
695 | to assist in making the interface between Perl and your extension simpler |
696 | or easier to understand. These routines should live in the .pm file. |
697 | Whether they are automatically loaded when the extension itself is loaded |
698 | or only loaded when called depends on where in the .pm file the subroutine |
699 | definition is placed. |
4633a7c4 |
700 | |
c07a80fd |
701 | =head2 DOCUMENTING YOUR EXTENSION |
4633a7c4 |
702 | |
c07a80fd |
703 | There is absolutely no excuse for not documenting your extension. |
704 | Documentation belongs in the .pm file. This file will be fed to pod2man, |
791fa977 |
705 | and the embedded documentation will be converted to the man page format, |
c07a80fd |
706 | then placed in the blib directory. It will be copied to Perl's man |
707 | page directory when the extension is installed. |
4633a7c4 |
708 | |
c07a80fd |
709 | You may intersperse documentation and Perl code within the .pm file. |
710 | In fact, if you want to use method autoloading, you must do this, |
711 | as the comment inside the .pm file explains. |
4633a7c4 |
712 | |
c07a80fd |
713 | See L<perlpod> for more information about the pod format. |
4633a7c4 |
714 | |
c07a80fd |
715 | =head2 INSTALLING YOUR EXTENSION |
4633a7c4 |
716 | |
c07a80fd |
717 | Once your extension is complete and passes all its tests, installing it |
718 | is quite simple: you simply run "make install". You will either need |
719 | to have write permission into the directories where Perl is installed, |
720 | or ask your system administrator to run the make for you. |
4633a7c4 |
721 | |
c07a80fd |
722 | =head2 SEE ALSO |
4633a7c4 |
723 | |
c07a80fd |
724 | For more information, consult L<perlguts>, L<perlxs>, L<perlmod>, |
725 | and L<perlpod>. |
4633a7c4 |
726 | |
c07a80fd |
727 | =head2 Author |
4633a7c4 |
728 | |
c07a80fd |
729 | Jeff Okamoto <okamoto@corp.hp.com> |
4633a7c4 |
730 | |
c07a80fd |
731 | Reviewed and assisted by Dean Roehrich, Ilya Zakharevich, Andreas Koenig, |
732 | and Tim Bunce. |
4633a7c4 |
733 | |
c07a80fd |
734 | =head2 Last Changed |
4633a7c4 |
735 | |
d9d2a7fb |
736 | 1996/7/10 |