Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

739 lines
25 KiB

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