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