Eliminate hints now correctly handled by Configure
[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
c07a80fd 8a Perl extension. The reader is assumed to have access to L<perlguts> and
4633a7c4 9L<perlxs>.
10
11This tutorial starts with very simple examples and becomes more complex,
c07a80fd 12with each new example adding new features. Certain concepts may not be
13completely explained until later in the tutorial in order to slowly ease
14the reader into building extensions.
4633a7c4 15
c07a80fd 16=head2 VERSION CAVEAT
4633a7c4 17
c07a80fd 18This tutorial tries hard to keep up with the latest development versions
19of Perl. This often means that it is sometimes in advance of the latest
20released version of Perl, and that certain features described here might
21not work on earlier versions. This section will keep track of when various
22features were added to Perl 5.
23
24=over 4
25
26=item *
27
791fa977 28In versions of 5.002 prior to version beta 3, then the line in the .xs file
29about "PROTOTYPES: DISABLE" will cause a compiler error. Simply remove that
30line from the file.
31
32=item *
33
34In versions of 5.002 prior to version 5.002b1h, the test.pl file was not
35automatically created by h2xs. This means that you cannot say "make test"
c07a80fd 36to 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
43In versions 5.000 and 5.001, instead of using the above line, you will need
44to use the following line:
45
46 BEGIN { unshift(@INC, "./blib") }
47
48=item *
49
50This document assumes that the executable named "perl" is Perl version 5.
51Some systems may have installed Perl version 5 as "perl5".
52
53=back
54
55=head2 DYNAMIC VERSUS STATIC
56
57It is commonly thought that if a system does not have the capability to
58dynamically load a library, you cannot build XSUB's. This is incorrect.
59You I<can> build them, but you must link the XSUB's subroutines with the
60rest of Perl, creating a new executable. This situation is similar to
61Perl 4.
62
63This tutorial can still be used on such a system. The XSUB build mechanism
64will check the system and build a dynamically-loadable library if possible,
65or else a static library and then, optionally, a new statically-linked
66executable with that static library linked in.
67
68Should you wish to build a statically-linked executable on a system which
69can dynamically load libraries, you may, in all the following examples,
70where the command "make" with no arguments is executed, run the command
71"make perl" instead.
72
73If you have generated such a statically-linked executable by choice, then
74instead of saying "make test", you should say "make test_static". On systems
75that cannot build dynamically-loadable libraries at all, simply saying "make
76test" is sufficient.
77
78=head2 EXAMPLE 1
4633a7c4 79
80Our first extension will be very simple. When we call the routine in the
c07a80fd 81extension, it will print out a well-known message and return.
4633a7c4 82
791fa977 83Run "h2xs -A -n Mytest". This creates a directory named Mytest, possibly under
c07a80fd 84ext/ if that directory exists in the current working directory. Several files
791fa977 85will be created in the Mytest dir, including MANIFEST, Makefile.PL, Mytest.pm,
86Mytest.xs, test.pl, and Changes.
4633a7c4 87
c07a80fd 88The MANIFEST file contains the names of all the files created.
4633a7c4 89
90The 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 103The 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 129And 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
145Let'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 152Now we'll run "perl Makefile.PL". This will create a real Makefile,
4633a7c4 153which 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 161Now, 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
175Now, although there is already a test.pl template ready for us, for this
176example only, we'll create a special test script. Create a file called hello
177that 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
187Now 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 195Now let's add to our extension a subroutine that will take a single argument
196and return 0 if the argument is even, 1 if the argument is odd.
4633a7c4 197
791fa977 198Add 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 208There does not need to be white space at the start of the "int input" line,
209but it is useful for improving readability. The semi-colon at the end of
210that line is also optional.
4633a7c4 211
c07a80fd 212Any white space may be between the "int" and "input". It is also okay for
213the four lines starting at the "CODE:" line to not be indented. However,
214for readability purposes, it is suggested that you indent them 8 spaces
215(or one normal tab stop).
4633a7c4 216
c07a80fd 217Now re-run make to rebuild our new shared library.
4633a7c4 218
c07a80fd 219Now perform the same steps as before, generating a Makefile from the
220Makefile.PL file, and running make.
4633a7c4 221
c07a80fd 222In order to test that our extension works, we now need to look at the
223file test.pl. This file is set up to imitate the same kind of testing
224structure that Perl itself has. Within the test script, you perform a
225number of tests to confirm the behavior of the extension, printing "ok"
226when the test is correct, "not ok" when it is not.
227
791fa977 228Remove the line that starts with "use lib", change the print statement in
229the BEGIN block to print "1..4", and add the following code to the end of
230the 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
236We will be calling the test script through the command "make test". You
237should 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
250The program h2xs is the starting point for creating extensions. In later
c07a80fd 251examples we'll see how we can use h2xs to read header files and generate
4633a7c4 252templates to connect to C routines.
253
254h2xs creates a number of files in the extension directory. The file
255Makefile.PL is a perl script which will generate a true Makefile to build
256the extension. We'll take a closer look at it later.
257
258The files <extension>.pm and <extension>.xs contain the meat of the extension.
259The .xs file holds the C routines that make up the extension. The .pm file
c07a80fd 260contains routines that tell Perl how to load your extension.
4633a7c4 261
c07a80fd 262Generating and invoking the Makefile created a directory blib (which stands
263for "build library") in the current working directory. This directory will
264contain the shared library that we will build. Once we have tested it, we
265can install it into its final location.
266
267Invoking the test script via "make test" did something very important. It
268invoked perl with all those -I arguments so that it could find the various
269files that are part of the extension.
270
271It is I<very> important that while you are still testing extensions that
272you use "make test". If you try to run the test script all by itself, you
273will get a fatal error.
274
275Another reason it is important to use "make test" to run your test script
276is 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
278version.
4633a7c4 279
c07a80fd 280When Perl sees a C<use extension;>, it searches for a file with the same name
281as the use'd extension that has a .pm suffix. If that file cannot be found,
282Perl dies with a fatal error. The default search path is contained in the
283@INC array.
4633a7c4 284
791fa977 285In our case, Mytest.pm tells perl that it will need the Exporter and Dynamic
c07a80fd 286Loader extensions. It then sets the @ISA and @EXPORT arrays and the $VERSION
287scalar; finally it tells perl to bootstrap the module. Perl will call its
288dynamic loader routine (if there is one) and load the shared library.
4633a7c4 289
c07a80fd 290The two arrays that are set in the .pm file are very important. The @ISA
291array contains a list of other packages in which to search for methods (or
292subroutines) that do not exist in the current package. The @EXPORT array
293tells Perl which of the extension's routines should be placed into the
294calling package's namespace.
4633a7c4 295
c07a80fd 296It's important to select what to export carefully. Do NOT export method names
297and do NOT export anything else I<by default> without a good reason.
4633a7c4 298
c07a80fd 299As a general rule, if the module is trying to be object-oriented then don't
300export anything. If it's just a collection of functions then you can export
301any of the functions via another array, called @EXPORT_OK.
4633a7c4 302
c07a80fd 303See L<perlmod> for more information.
4633a7c4 304
c07a80fd 305The $VERSION variable is used to ensure that the .pm file and the shared
791fa977 306library are "in sync" with each other. Any time you make changes to
307the .pm or .xs files, you should increment the value of this variable.
308
309=head2 WRITING GOOD TEST SCRIPTS
310
311The importance of writing good test scripts cannot be overemphasized. You
312should closely follow the "ok/not ok" style that Perl itself uses, so that
313it is very easy and unambiguous to determine the outcome of each test case.
314When you find and fix a bug, make sure you add a test case for it.
315
316By running "make test", you ensure that your test.pl script runs and uses
317the correct version of your extension. If you have many test cases, you
318might want to copy Perl's test style. Create a directory named "t", and
319ensure all your test files end with the suffix ".t". The Makefile will
320properly run all these test files.
321
4633a7c4 322
c07a80fd 323=head2 EXAMPLE 3
4633a7c4 324
325Our third extension will take one argument as its input, round off that
c07a80fd 326value, and set the I<argument> to the rounded value.
4633a7c4 327
791fa977 328Add 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 344Edit the Makefile.PL file so that the corresponding line looks like this:
4633a7c4 345
346 'LIBS' => ['-lm'], # e.g., '-lm'
347
c07a80fd 348Generate 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
357Running "make test" should now print out that all nine tests are okay.
4633a7c4 358
c07a80fd 359You might be wondering if you can round a constant. To see what happens, add
360the following line to test.pl temporarily:
4633a7c4 361
791fa977 362 &Mytest::round(3);
4633a7c4 363
c07a80fd 364Run "make test" and notice that Perl dies with a fatal error. Perl won't let
365you change the value of constants!
4633a7c4 366
c07a80fd 367=head2 WHAT'S NEW HERE?
4633a7c4 368
369Two things are new here. First, we've made some changes to Makefile.PL.
370In this case, we've specified an extra library to link in, in this case the
371math library, libm. We'll talk later about how to write XSUBs that can call
372every routine in a library.
373
374Second, the value of the function is being passed back not as the function's
375return value, but through the same variable that was passed into the function.
376
c07a80fd 377=head2 INPUT AND OUTPUT PARAMETERS
4633a7c4 378
379You specify the parameters that will be passed into the XSUB just after you
791fa977 380declare the function return value and name. Each parameter line starts with
381optional white space, and may have an optional terminating semicolon.
4633a7c4 382
383The list of output parameters occurs after the OUTPUT: directive. The use
384of RETVAL tells Perl that you wish to send this value back as the return
c07a80fd 385value of the XSUB function. In Example 3, the value we wanted returned was
386contained in the same variable we passed in, so we listed it (and not RETVAL)
387in the OUTPUT: section.
4633a7c4 388
c07a80fd 389=head2 THE XSUBPP COMPILER
4633a7c4 390
391The compiler xsubpp takes the XS code in the .xs file and converts it into
392C code, placing it in a file whose suffix is .c. The C code created makes
393heavy use of the C functions within Perl.
394
c07a80fd 395=head2 THE TYPEMAP FILE
4633a7c4 396
397The xsubpp compiler uses rules to convert from Perl's data types (scalar,
398array, etc.) to C's data types (int, char *, etc.). These rules are stored
399in the typemap file ($PERLLIB/ExtUtils/typemap). This file is split into
400three parts.
401
402The first part attempts to map various C data types to a coded flag, which
403has some correspondence with the various Perl types. The second part contains
404C code which xsubpp uses for input parameters. The third part contains C
405code which xsubpp uses for output parameters. We'll talk more about the
406C code later.
407
c07a80fd 408Let'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
429Notice the two lines marked with "XXXXX". If you check the first section of
430the typemap file, you'll see that doubles are of type T_DOUBLE. In the
431INPUT section, an argument that is T_DOUBLE is assigned to the variable
432arg by calling the routine SvNV on something, then casting it to double,
433then assigned to the variable arg. Similarly, in the OUTPUT section,
434once arg has its final value, it is passed to the sv_setnv function to
435be passed back to the calling subroutine. These two functions are explained
c07a80fd 436in L<perlguts>; we'll talk more later about what that "ST(0)" means in the
4633a7c4 437section on the argument stack.
438
c07a80fd 439=head2 WARNING
4633a7c4 440
c07a80fd 441In general, it's not a good idea to write extensions that modify their input
4633a7c4 442parameters, as in Example 3. However, in order to better accomodate calling
443pre-existing C routines, which often do modify their input parameters,
791fa977 444this behavior is tolerated.
445
446=head2 EXAMPLE 4
447
448In this example, we'll now begin to write XSUB's that will interact with
449pre-defined C libraries. To begin with, we will build a small library of
450our own, then let h2xs write our .pm and .xs files for us.
451
452Create a new directory called Mytest2 at the same level as the directory
453Mytest. In the Mytest2 directory, create another directory called mylib,
454and cd into that directory.
455
456Here we'll create some files that will generate a test library. These will
457include a C source file and a header file. We'll also create a Makefile.PL
458in this directory. Then we'll make sure that running make at the Mytest2
459level will automatically run this Makefile.PL file and the resulting Makefile.
460
461In 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
467Also 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
481And 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
504We will now create the main top-level Mytest2 files. Change to the directory
505above Mytest2 and run the following command:
506
507 % h2xs -O -n Mytest2 < ./Mytest2/mylib/mylib.h
508
509This will print out a warning about overwriting Mytest2, but that's okay.
510Our files are stored in Mytest2/mylib, and will be untouched.
511
512The normal Makefile.PL that h2xs generates doesn't know about the mylib
513directory. We need to tell it that there is a subdirectory and that we
514will be generating a library in it. Let's add the following key-value
515pair to the WriteMakefile call:
4633a7c4 516
791fa977 517 'MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)',
518
519and 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
529the line "cd mylib && $(MAKE)".)
530
531Let's also fix the MANIFEST file so that it accurately reflects the contents
532of our extension. The single line that says "mylib" should be replaced by
533the following three lines:
534
535 mylib/Makefile.PL
536 mylib/mylib.c
537 mylib/mylib.h
538
539To keep our namespace nice and unpolluted, edit the .pm file and change
540the line setting @EXPORT to @EXPORT_OK. And finally, in the .xs file,
541edit the #include line to read:
542
543 #include "mylib/mylib.h"
544
545And 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
555Now we also need to create a typemap file because the default Perl doesn't
556currently support the const char * type. Create a file called typemap and
557place the following in it:
558
559 const char * T_PV
560
561Now run perl on the top-level Makefile.PL. Notice that it also created a
562Makefile in the mylib directory. Run make and see that it does cd into
563the mylib directory and run make in there as well.
564
565Now edit the test.pl script and change the BEGIN block to print "1..4",
566and 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
573for equality, but rather the difference being below a certain epsilon factor,
5740.01 in this case)
575
576Run "make test" and all should be well.
577
578=head 2 WHAT HAS HAPPENED HERE?
579
580Unlike previous examples, we've now run h2xs on a real include file. This
581has caused some extra goodies to appear in both the .pm and .xs files.
582
583=item *
584
585In the .xs file, there's now a #include declaration with the full path to
586the mylib.h header file.
587
588=item *
589
590There's now some new C code that's been added to the .xs file. The purpose
591of the C<constant> routine is to make the values that are #define'd in the
592header file available to the Perl script (in this case, by calling
593C<&main::TESTVAL>). There's also some XS code to allow calls to the
594C<constant> routine.
595
596=item *
597
598The .pm file has exported the name TESTVAL in the @EXPORT array. This
599could lead to name clashes. A good rule of thumb is that if the #define
600is only going to be used by the C routines themselves, and not by the user,
601they should be removed from the @EXPORT array. Alternately, if you don't
602mind using the "fully qualified name" of a variable, you could remove most
603or all of the items in the @EXPORT array.
604
605=back
606
607We've also told Perl about the library that we built in the mylib
608subdirectory. That required only the addition of the MYEXTLIB variable
609to the WriteMakefile call and the replacement of the postamble subroutine
610to cd into the subdirectory and run make. The Makefile.PL for the
611library is a bit more complicated, but not excessively so. Again we
612replaced the postamble subroutine to insert our own code. This code
613simply specified that the library to be created here was a static
614archive (as opposed to a dynamically loadable library) and provided the
615commands to build it.
4633a7c4 616
c07a80fd 617=head2 SPECIFYING ARGUMENTS TO XSUBPP
4633a7c4 618
791fa977 619With the completion of Example 4, we now have an easy way to simulate some
c07a80fd 620real-life libraries whose interfaces may not be the cleanest in the world.
621We shall now continue with a discussion of the arguments passed to the
622xsubpp compiler.
4633a7c4 623
c07a80fd 624When you specify arguments in the .xs file, you are really passing three
625pieces of information for each one listed. The first piece is the order
626of that argument relative to the others (first, second, etc). The second
627is the type of argument, and consists of the type declaration of the
628argument (e.g., int, char*, etc). The third piece is the exact way in
629which the argument should be used in the call to the library function
630from this XSUB. This would mean whether or not to place a "&" before
631the argument or not, meaning the argument expects to be passed the address
632of the specified data type.
4633a7c4 633
c07a80fd 634There 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 641The first argument to this function would be treated as a char and assigned
642to the variable a, and its address would be passed into the function foo.
643The second argument would be treated as a string pointer and assigned to the
644variable b. The I<value> of b would be passed into the function foo. The
645actual call to the function foo that xsubpp generates would look like this:
4633a7c4 646
c07a80fd 647 foo(&a, b);
4633a7c4 648
791fa977 649Xsubpp will identically parse the following function argument lists:
650
651 char &a
652 char&a
653 char & a
654
655However, to help ease understanding, it is suggested that you place a "&"
656next 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
658complete example above). By doing so, it is easy to understand exactly
659what will be passed to the C function -- it will be whatever is in the
660"last column".
4633a7c4 661
c07a80fd 662You should take great pains to try to pass the function the type of variable
663it 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 667If we look at any of the C code generated by any of the examples except
668example 1, you will notice a number of references to ST(n), where n is
669usually 0. The "ST" is actually a macro that points to the n'th argument
670on the argument stack. ST(0) is thus the first argument passed to the
671XSUB, ST(1) is the second argument, and so on.
4633a7c4 672
c07a80fd 673When you list the arguments to the XSUB in the .xs file, that tell xsubpp
674which argument corresponds to which of the argument stack (i.e., the first
675one listed is the first argument, and so on). You invite disaster if you
676do not list them in the same order as the function expects them.
4633a7c4 677
c07a80fd 678=head2 EXTENDING YOUR EXTENSION
4633a7c4 679
c07a80fd 680Sometimes you might want to provide some extra methods or subroutines
681to assist in making the interface between Perl and your extension simpler
682or easier to understand. These routines should live in the .pm file.
683Whether they are automatically loaded when the extension itself is loaded
684or only loaded when called depends on where in the .pm file the subroutine
685definition is placed.
4633a7c4 686
c07a80fd 687=head2 DOCUMENTING YOUR EXTENSION
4633a7c4 688
c07a80fd 689There is absolutely no excuse for not documenting your extension.
690Documentation belongs in the .pm file. This file will be fed to pod2man,
791fa977 691and the embedded documentation will be converted to the man page format,
c07a80fd 692then placed in the blib directory. It will be copied to Perl's man
693page directory when the extension is installed.
4633a7c4 694
c07a80fd 695You may intersperse documentation and Perl code within the .pm file.
696In fact, if you want to use method autoloading, you must do this,
697as the comment inside the .pm file explains.
4633a7c4 698
c07a80fd 699See L<perlpod> for more information about the pod format.
4633a7c4 700
c07a80fd 701=head2 INSTALLING YOUR EXTENSION
4633a7c4 702
c07a80fd 703Once your extension is complete and passes all its tests, installing it
704is quite simple: you simply run "make install". You will either need
705to have write permission into the directories where Perl is installed,
706or ask your system administrator to run the make for you.
4633a7c4 707
c07a80fd 708=head2 SEE ALSO
4633a7c4 709
c07a80fd 710For more information, consult L<perlguts>, L<perlxs>, L<perlmod>,
711and L<perlpod>.
4633a7c4 712
c07a80fd 713=head2 Author
4633a7c4 714
c07a80fd 715Jeff Okamoto <okamoto@corp.hp.com>
4633a7c4 716
c07a80fd 717Reviewed and assisted by Dean Roehrich, Ilya Zakharevich, Andreas Koenig,
718and Tim Bunce.
4633a7c4 719
c07a80fd 720=head2 Last Changed
4633a7c4 721
791fa977 7221996/2/9