home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / SLAKWARE / D13 / PERL2.TGZ / perl2.tar / usr / lib / perl5 / pod / perlxstut.pod < prev   
Text File  |  1996-06-28  |  25KB  |  723 lines

  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
  8. a Perl extension.  The reader is assumed to have access to L<perlguts> and
  9. L<perlxs>.
  10.  
  11. This tutorial starts with very simple examples and becomes more complex,
  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.
  15.  
  16. =head2 VERSION CAVEAT
  17.  
  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.  
  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"
  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
  79.  
  80. Our first extension will be very simple.  When we call the routine in the
  81. extension, it will print out a well-known message and return.
  82.  
  83. Run "h2xs -A -n Mytest".  This creates a directory named Mytest, possibly under
  84. ext/ if that directory exists in the current working directory.  Several files
  85. will be created in the Mytest dir, including MANIFEST, Makefile.PL, Mytest.pm,
  86. Mytest.xs, test.pl, and Changes.
  87.  
  88. The MANIFEST file contains the names of all the files created.
  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(
  96.         'NAME'      => 'Mytest',
  97.         'VERSION_FROM' => 'Mytest.pm', # finds $VERSION
  98.         'LIBS'      => [''],   # e.g., '-lm'
  99.         'DEFINE'    => '',     # e.g., '-DHAVE_SOMETHING'
  100.         'INC'       => '',     # e.g., '-I/usr/include/other'
  101.     );
  102.  
  103. The file Mytest.pm should start with something like this:
  104.  
  105.     package Mytest;
  106.  
  107.     require Exporter;
  108.     require DynaLoader;
  109.  
  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(
  115.  
  116.     );
  117.     $VERSION = '0.01';
  118.  
  119.     bootstrap Mytest $VERSION;
  120.  
  121.     # Preloaded methods go here.
  122.  
  123.     # Autoload methods go after __END__, and are processed by the autosplit program.
  124.  
  125.     1;
  126.     __END__
  127.     # Below is the stub of documentation for your module. You better edit it!
  128.  
  129. And the Mytest.xs file should look something like this:
  130.  
  131.     #ifdef __cplusplus
  132.     extern "C" {
  133.     #endif
  134.     #include "EXTERN.h"
  135.     #include "perl.h"
  136.     #include "XSUB.h"
  137.     #ifdef __cplusplus
  138.     }
  139.     #endif
  140.     
  141.     PROTOTYPES: DISABLE
  142.  
  143.     MODULE = Mytest        PACKAGE = Mytest
  144.  
  145. Let's edit the .xs file by adding this to the end of the file:
  146.  
  147.     void
  148.     hello()
  149.         CODE:
  150.         printf("Hello, world!\n");
  151.  
  152. Now we'll run "perl Makefile.PL".  This will create a real Makefile,
  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
  158.     Writing Makefile for Mytest
  159.     %
  160.  
  161. Now, running make will produce output that looks something like this
  162. (some long lines shortened for clarity):
  163.  
  164.     % make
  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
  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.  
  179.     #! /opt/perl5/bin/perl
  180.     
  181.     use lib './blib';
  182.     
  183.     use Mytest;
  184.     
  185.     Mytest::hello();
  186.  
  187. Now we run the script and we should see the following output:
  188.  
  189.     % perl hello
  190.     Hello, world!
  191.     %
  192.  
  193. =head2 EXAMPLE 2
  194.  
  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.
  197.  
  198. Add the following to the end of Mytest.xs:
  199.  
  200.     int
  201.     is_even(input)
  202.         int    input
  203.         CODE:
  204.         RETVAL = (input % 2 == 0);
  205.         OUTPUT:
  206.         RETVAL
  207.  
  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.
  211.  
  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).
  216.  
  217. Now re-run make to rebuild our new shared library.
  218.  
  219. Now perform the same steps as before, generating a Makefile from the
  220. Makefile.PL file, and running make.
  221.  
  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.  
  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:
  231.  
  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";
  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
  246.     %
  247.  
  248. =head2 WHAT HAS GONE ON?
  249.  
  250. The program h2xs is the starting point for creating extensions.  In later
  251. examples we'll see how we can use h2xs to read header files and generate
  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
  260. contains routines that tell Perl how to load your extension.
  261.  
  262. G