Updated.
[p5sagit/p5-mst-13.2.git] / pod / perlxstut.pod
CommitLineData
4633a7c4 1=head1 NAME
2
3perlXStut - Tutorial for XSUB's
4
5=head1 DESCRIPTION
6
7This tutorial will educate the reader on the steps involved in creating
8a Perl 5 extension. The reader is assumed to have access to L<perlguts> and
9L<perlxs>.
10
11This tutorial starts with very simple examples and becomes more complex,
12bringing in more features that are available. Thus, certain statements
13towards the beginning may be incomplete. The reader is encouraged to
14read the entire document before lambasting the author about apparent
15mistakes.
16
17This tutorial is still under construction. Constructive comments
18are welcome.
19
20=head1 EXAMPLE 1
21
22Our first extension will be very simple. When we call the routine in the
23extension, it will print out a well-known message and terminate.
24
cb1a09d0 25Run C<h2xs -A -n Test1>. This creates a directory named Test1, possibly under
4633a7c4 26ext/ if it exists in the current working directory. Four files will be
27created in the Test1 dir: MANIFEST, Makefile.PL, Test1.pm, Test1.xs.
28
29The MANIFEST file should contain the names of the four files created.
30
31The file Makefile.PL should look something like this:
32
33 use ExtUtils::MakeMaker;
34 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
35 # the contents of the Makefile that is written.
36 WriteMakefile(
37 'NAME' => 'Test1',
38 'VERSION' => '0.1',
39 'LIBS' => [''], # e.g., '-lm'
40 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING'
41 'INC' => '', # e.g., '-I/usr/include/other'
42 );
43
44The file Test1.pm should look something like this:
45
46 package Test1;
47
48 require Exporter;
49 require DynaLoader;
50
51 @ISA = qw(Exporter DynaLoader);
52 # Items to export into callers namespace by default. Note: do not export
53 # names by default without a very good reason. Use EXPORT_OK instead.
54 # Do not simply export all your public functions/methods/constants.
55 @EXPORT = qw(
56
57 );
58 bootstrap Test1;
59
60 # Preloaded methods go here.
61
62 # Autoload methods go after __END__, and are processed by the autosplit program.
63
64 1;
65 __END__
66
67And the Test1.xs file should look something like this:
68
69 #include "EXTERN.h"
70 #include "perl.h"
71 #include "XSUB.h"
72
73 MODULE = Test1 PACKAGE = Test1
74
75Let's edit the .xs file by adding this to the end of the file:
76
77 void
78 hello()
79
80 CODE:
81 printf("Hello, world!\n");
82
cb1a09d0 83Now we'll run C<perl Makefile.PL>. This will create a real Makefile,
4633a7c4 84which make needs. It's output looks something like:
85
86 % perl Makefile.PL
87 Checking if your kit is complete...
88 Looks good
89 Writing Makefile for Test1
90 %
91
92Now, running make will produce output that looks something like this:
93
94 % make
95 mkdir ./blib
96 mkdir ./blib/auto
97 mkdir ./blib/auto/Test1
98 perl xsubpp -typemap typemap Test1.xs >Test1.tc && mv Test1.tc Test1.c
99 cc -c Test1.c
100 Running Mkbootstrap for Test1 ()
101 chmod 644 Test1.bs
102 LD_RUN_PATH="" ld -o ./blib/auto/Test1/Test1.sl -b Test1.o
103 chmod 755 ./blib/auto/Test1/Test1.sl
104 cp Test1.bs ./blib/auto/Test1/Test1.bs
105 chmod 644 ./blib/auto/Test1/Test1.bs
106 cp Test1.pm ./blib/Test1.pm
107 chmod 644 ./blib/Test1.pm
108
109Now we'll create a test script, test1.pl in the Test1 directory. It should
110look like this:
111
112 #! /usr/local/bin/perl
113
114 BEGIN { unshift(@INC, "./blib") }
115
116 use Test1;
117
118 Test1::hello();
119
120Now we run the script and we should see the following output:
121
122 % perl test1.pl
123 Hello, world!
124 %
125
126=head1 EXAMPLE 2
127
128Now let's create a simple extension that will take a single argument and
129return 0 if the argument is even, 1 if the argument is odd.
130
cb1a09d0 131Run C<h2xs -A -n Test2>. This will create a Test2 directory with a file
4633a7c4 132Test2.xs underneath it. Add the following to the end of the XS file:
133
134 int
135 is_even(input)
136 int input
137
138 CODE:
139 RETVAL = input % 2;
140
141 OUTPUT:
142 RETVAL
143
144(Note that the line after the declaration of is_even is indented one tab
145stop. Although there is a tab between "int" and "input", this can be any
146amount of white space. Also notice that there is no semi-colon following
147the "declaration" of the variable input)
148
149Now perform the same steps before, generating a Makefile from the
150Makefile.PL file, and running make.
151
152Our test file test2.pl will now look like:
153
154 BEGIN { unshift(@INC, "./blib"); }
155
156 use Test2;
157
158 $a = &Test2::is_even(2);
159 $b = &Test2::is_even(3);
160
161 print "\$a is $a, \$b is $b\n";
162
163The output should look like:
164
165 % perl test2.pl
166 $a is 0, $b is 1
167 %
168
169=head1 WHAT HAS GONE ON?
170
171The program h2xs is the starting point for creating extensions. In later
172examples, we'll see how we can use h2xs to read header files and generate
173templates to connect to C routines.
174
175h2xs creates a number of files in the extension directory. The file
176Makefile.PL is a perl script which will generate a true Makefile to build
177the extension. We'll take a closer look at it later.
178
179The files <extension>.pm and <extension>.xs contain the meat of the extension.
180The .xs file holds the C routines that make up the extension. The .pm file
181contains routines that tells Perl how to load your extension.
182
183Generating the invoking the Makefile created a directory blib in the current
184working directory. This directory will contain the shared library that we
185will build. Once we have tested it, we can install it into its final location.
186
187Finally, our test scripts do two important things. First of all, they place
188the directory "blib" at the head of the @INC array. Placing this inside a
189BEGIN block assures us that Perl will look in the blib directory hierarchy
190before looking in the system directories. This could be important if you are
191upgrading an already-existing extension and do not want to disturb the system
192version until you are ready to install it.
193
194Second, the test scripts tell Perl to C<use extension;>. When Perl sees this,
195it searches for a .pm file of the same name in the various directories kept
196in the @INC array. If it cannot be found, perl will die with an error that
197will look something like:
198
199 Can't locate Test2.pm in @INC at ./test2.pl line 5.
200 BEGIN failed--compilation aborted at ./test2.pl line 5.
201
202The .pm file tells perl that it will need the Exporter and Dynamic Loader
203extensions. It then sets the @ISA array, which is used for looking up
204methods that might not exist in the current package, and finally tells perl
205to bootstrap the module. Perl will call its dynamic loader routine and load
206the shared library.
207
208The @EXPORT array in the .pm file tells Perl which of the extension's
209routines should be placed into the calling package's namespace. In our two
210examples so far, we have not modified the @EXPORT array, so our test
211scripts must call the routines by their complete name (e.g., Test1::hello).
212If we placed the name of the routine in the @EXPORT array, so that the
213.pm file looked like:
214
215 @EXPORT = qw( hello );
216
217Then the hello routine would also be callable from the "main" package.
218We could therefore change test1.pl to look like:
219
220 #! /usr/local/bin/perl
221
222 BEGIN { unshift(@INC, "./blib") }
223
224 use Test1;
225
226 hello();
227
228And we would get the same output, "Hello, world!".
229
230Most of the time you do not want to export the names of your extension's
231subroutines, because they might accidentally clash with other subroutines
232from other extensions or from the calling program itself.
233
234=head1 EXAMPLE 3
235
236Our third extension will take one argument as its input, round off that
237value, and set the argument to the rounded value.
238
cb1a09d0 239Run C<h2xs -A -n Test3>. This will create a Test3 directory with a file
4633a7c4 240Test3.xs underneath it. Add the following to the end of the XS file:
241
242 void
243 round(arg)
244 double arg
245
246 CODE:
247 if (arg > 0.0) {
248 arg = floor(arg + 0.5);
249 } else if (arg < 0.0) {
250 arg = ceil(arg - 0.5);
251 } else {
252 arg = 0.0;
253 }
254 OUTPUT:
255 arg
256
257Edit the file Makefile.PL so that the corresponding line looks like this:
258
259 'LIBS' => ['-lm'], # e.g., '-lm'
260
261Generate the Makefile and run make. The test script test3.pl looks like:
262
263 #! /usr/local/bin/perl
264
265 BEGIN { unshift(@INC, "./blib"); }
266
267 use Test3;
268
269 foreach $i (-1.4, -0.5, 0.0, 0.4, 0.5) {
270 $j = $i;
271 &Test3::round($j);
272 print "Rounding $i results in $j\n";
273 }
274
275 print STDERR "Trying to round a constant -- ";
276 &Test3::round(2.0);
277
278Notice the output from trying to send a constant in to the routine. Perl
279reports:
280
281 Modification of a read-only value attempted at ./test3.pl line 15.
282
283Perl won't let you change the value of two to, say, three, unlike a FORTRAN
284compiler from long, long ago!
285
286=head1 WHAT'S NEW HERE?
287
288Two things are new here. First, we've made some changes to Makefile.PL.
289In this case, we've specified an extra library to link in, in this case the
290math library, libm. We'll talk later about how to write XSUBs that can call
291every routine in a library.
292
293Second, the value of the function is being passed back not as the function's
294return value, but through the same variable that was passed into the function.
295
296=head1 INPUT AND OUTPUT PARAMETERS
297
298You specify the parameters that will be passed into the XSUB just after you
299declare the function return value and name. The list of parameters looks
300very C-like, but the lines must be indented by a tab stop, and each line
301may not have an ending semi-colon.
302
303The list of output parameters occurs after the OUTPUT: directive. The use
304of RETVAL tells Perl that you wish to send this value back as the return
305value of the XSUB function. Otherwise, you specify which variables used
306in the XSUB function should be placed into the respective Perl variables
307passed in.
308
309=head1 THE XSUBPP COMPILER
310
311The compiler xsubpp takes the XS code in the .xs file and converts it into
312C code, placing it in a file whose suffix is .c. The C code created makes
313heavy use of the C functions within Perl.
314
315=head1 THE TYPEMAP FILE
316
317The xsubpp compiler uses rules to convert from Perl's data types (scalar,
318array, etc.) to C's data types (int, char *, etc.). These rules are stored
319in the typemap file ($PERLLIB/ExtUtils/typemap). This file is split into
320three parts.
321
322The first part attempts to map various C data types to a coded flag, which
323has some correspondence with the various Perl types. The second part contains
324C code which xsubpp uses for input parameters. The third part contains C
325code which xsubpp uses for output parameters. We'll talk more about the
326C code later.
327
328Let's now take a look at the .c file created for the Test3 extension.
329
330 /*
331 * This file was generated automatically by xsubpp version 1.9 from the
332 * contents of Test3.xs. Don't edit this file, edit Test3.xs instead.
333 *
334 * ANY CHANGES MADE HERE WILL BE LOST!
335 *
336 */
337
338 #include "EXTERN.h"
339 #include "perl.h"
340 #include "XSUB.h"
341
342
343 XS(XS_Test3_round)
344 {
345 dXSARGS;
346 if (items != 1) {
347 croak("Usage: Test3::round(arg)");
348 }
349 {
350 double arg = (double)SvNV(ST(0)); /* XXXXX */
351
352 if (arg > 0.0) {
353 arg = floor(arg + 0.5);
354 } else if (arg < 0.0) {
355 arg = ceil(arg - 0.5);
356 }
357
358 sv_setnv(ST(0), (double)arg); /* XXXXX */
359 }
360 XSRETURN(1);
361 }
362
363 XS(boot_Test3)
364 {
365 dXSARGS;
366 char* file = __FILE__;
367
368 newXS("Test3::round", XS_Test3_round, file);
369 ST(0) = &sv_yes;
370 XSRETURN(1);
371 }
372
373Notice the two lines marked with "XXXXX". If you check the first section of
374the typemap file, you'll see that doubles are of type T_DOUBLE. In the
375INPUT section, an argument that is T_DOUBLE is assigned to the variable
376arg by calling the routine SvNV on something, then casting it to double,
377then assigned to the variable arg. Similarly, in the OUTPUT section,
378once arg has its final value, it is passed to the sv_setnv function to
379be passed back to the calling subroutine. These two functions are explained
380in perlguts; we'll talk more later about what that "ST(0)" means in the
381section on the argument stack.
382
383=head1 WARNING
384
385In general, it's not agood idea to write extensions that modify their input
386parameters, as in Example 3. However, in order to better accomodate calling
387pre-existing C routines, which often do modify their input parameters,
388this behavior is tolerated. The next example will show to do this.
389
390=head1 EXAMPLE 4
391
392We'll now show how we can call routines in libraries, such as the curses
393screen handling package, or a DBM module like GDBM. Each of these libraries
394has a header file from which we will generate an XS template that we'll then
395fine-tune.
396
397Rather than attempt to find a library that exists on all systems, we'll
398first create our own C library, then create an XSUB to it.
399
400Let's create the files libtest4.h and libtest4.c as follows:
401
402 /* libtest4.h */
403
404 #define TESTVAL 4
405
406 extern int test4(int, long, const char*);
407
408 /* libtest4.c */
409
410 #include <stdlib.h>
411 #include "./libtest4.h"
412
413 int
414 test4(a, b, c)
415 int a;
416 long b;
417 const char * c;
418 {
419 return (a + b + atof(c) + TESTVAL);
420 }
421
422Now let's compile it into a library. Since we'll be eventually using this
423archive to create a shared library, be sure to use the correct flags to
424generate position-independent code. In HP-UX, that's:
425
426 % cc -Aa -D_HPUX_SOURCE -c +z libtest4.c
427 % ar cr libtest4.a libtest4.o
428
429Now let's move the libtest4.h and libtest.a files into a sub-directory under
430/tmp, so we don't interfere with anything.
431
432 % mkdir /tmp/test4
433 % mkdir /tmp/test4/include
434 % mkdir /tmp/test4/lib
435 % cp libtest4.h /tmp/test4/include
436 % cp libtest4.a /tmp/test4/lib
437
438Okay, now that we have a header file and a library, let's begin actually
439writing the extension.
440
cb1a09d0 441Run C<h2xs -n Test4 /tmp/test4/include/libtest4.h> (notice we are no longer
442specifying B<-A> as an argument). This will create a Test4 directory with a file
443F<Test4.xs> underneath it. If we look at it now, we'll see some interesting
4633a7c4 444things have been added to the various files.
445
446=over 2
447
448=item *
449
450In the .xs file, there's now a #include declaration with the full path to
451the libtest4.h header file.
452
453=item *
454
455There's now some new C code that's been added to the .xs file. The purpose
456of the C<constant> routine is to make the values that are #define'd in the
457header file available to the Perl script by calling C<&main::TESTVAL>.
458There's also some XS code to allow calls to the C<constant> routine.
459
460=item *
461
462The .pm file has exported the name TESTVAL in the @EXPORT array. This
463could lead to name clashes. A good rule of thumb is that if the #define
464is only going to be used by the C routines themselves, and not by the user,
465they should be removed from the @EXPORT array. Alternately, if you don't
466mind using the "fully qualified name" of a variable, you could remove most
467or all of the items in the @EXPORT array.
468
469=back
470
471Let's now add a definition for the routine in our library. Add the following
472code to the end of the .xs file:
473
474 int
475 test4(a,b,c)
476 int a
477 long b
478 const char * c
479
480Now we also need to create a typemap file because the default Perl doesn't
481currently support the const char * type. Create a file called typemap and
482place the following in it:
483
484 const char * T_PV
485
486Now we must tell our Makefile template where our new library is. Edit the
487Makefile.PL and change the following line:
488
489 'LIBS' => ['-ltest4 -L/tmp/test4'], # e.g., '-lm'
490
491This specifies that we want the library test4 linked into our XSUB, and that
492it should also look in the directory /tmp/test4.
493
494Let's also change the following line in the Makefile.PL to this:
495
496 'INC' => '-I/tmp/test/include', # e.g., '-I/usr/include/other'
497
498and also change the #include in test4.xs to be:
499
500 #include <libtest4.h>
501
502Now we don't have to specify the absolute path of the header file in the
503.xs file, relying on the Makefile to tell the compiler where to find the
504header files. This is generally considered a Good Thing.
505
506Okay, let's create the Makefile, and run make. You can ignore a message that
507may look like:
508
509 Warning (non-fatal): No library found for -ltest4
510
511If you forgot to create the typemap file, you might see output that looks
512like this:
513
514 Error: 'const char *' not in typemap in test4.xs, line 102
515
516This error means that you have used a C datatype that xsubpp doesn't know
517how to convert between Perl and C. You'll have to create a typemap file to
518tell xsubpp how to do the conversions.
519
520=head1 Author
521
522Jeff Okamoto
523
524=head1 Last Changed
525
5261995/11/20
527
528Jeff Okamoto
529F<E<lt>okamoto@hpcc123.corp.hp.comE<gt>>