| 1 | =head1 NAME
|
|---|
| 2 |
|
|---|
| 3 | perlXStut - Tutorial for writing XSUBs
|
|---|
| 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>,
|
|---|
| 9 | L<perlapi> and 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 | This tutorial was written from a Unix point of view. Where I know them
|
|---|
| 17 | to be otherwise different for other platforms (e.g. Win32), I will list
|
|---|
| 18 | them. If you find something that was missed, please let me know.
|
|---|
| 19 |
|
|---|
| 20 | =head1 SPECIAL NOTES
|
|---|
| 21 |
|
|---|
| 22 | =head2 make
|
|---|
| 23 |
|
|---|
| 24 | This tutorial assumes that the make program that Perl is configured to
|
|---|
| 25 | use is called C<make>. Instead of running "make" in the examples that
|
|---|
| 26 | follow, you may have to substitute whatever make program Perl has been
|
|---|
| 27 | configured to use. Running B<perl -V:make> should tell you what it is.
|
|---|
| 28 |
|
|---|
| 29 | =head2 Version caveat
|
|---|
| 30 |
|
|---|
| 31 | When writing a Perl extension for general consumption, one should expect that
|
|---|
| 32 | the extension will be used with versions of Perl different from the
|
|---|
| 33 | version available on your machine. Since you are reading this document,
|
|---|
| 34 | the version of Perl on your machine is probably 5.005 or later, but the users
|
|---|
| 35 | of your extension may have more ancient versions.
|
|---|
| 36 |
|
|---|
| 37 | To understand what kinds of incompatibilities one may expect, and in the rare
|
|---|
| 38 | case that the version of Perl on your machine is older than this document,
|
|---|
| 39 | see the section on "Troubleshooting these Examples" for more information.
|
|---|
| 40 |
|
|---|
| 41 | If your extension uses some features of Perl which are not available on older
|
|---|
| 42 | releases of Perl, your users would appreciate an early meaningful warning.
|
|---|
| 43 | You would probably put this information into the F<README> file, but nowadays
|
|---|
| 44 | installation of extensions may be performed automatically, guided by F<CPAN.pm>
|
|---|
| 45 | module or other tools.
|
|---|
| 46 |
|
|---|
| 47 | In MakeMaker-based installations, F<Makefile.PL> provides the earliest
|
|---|
| 48 | opportunity to perform version checks. One can put something like this
|
|---|
| 49 | in F<Makefile.PL> for this purpose:
|
|---|
| 50 |
|
|---|
| 51 | eval { require 5.007 }
|
|---|
| 52 | or die <<EOD;
|
|---|
| 53 | ############
|
|---|
| 54 | ### This module uses frobnication framework which is not available before
|
|---|
| 55 | ### version 5.007 of Perl. Upgrade your Perl before installing Kara::Mba.
|
|---|
| 56 | ############
|
|---|
| 57 | EOD
|
|---|
| 58 |
|
|---|
| 59 | =head2 Dynamic Loading versus Static Loading
|
|---|
| 60 |
|
|---|
| 61 | It is commonly thought that if a system does not have the capability to
|
|---|
| 62 | dynamically load a library, you cannot build XSUBs. This is incorrect.
|
|---|
| 63 | You I<can> build them, but you must link the XSUBs subroutines with the
|
|---|
| 64 | rest of Perl, creating a new executable. This situation is similar to
|
|---|
| 65 | Perl 4.
|
|---|
| 66 |
|
|---|
| 67 | This tutorial can still be used on such a system. The XSUB build mechanism
|
|---|
| 68 | will check the system and build a dynamically-loadable library if possible,
|
|---|
| 69 | or else a static library and then, optionally, a new statically-linked
|
|---|
| 70 | executable with that static library linked in.
|
|---|
| 71 |
|
|---|
| 72 | Should you wish to build a statically-linked executable on a system which
|
|---|
| 73 | can dynamically load libraries, you may, in all the following examples,
|
|---|
| 74 | where the command "C<make>" with no arguments is executed, run the command
|
|---|
| 75 | "C<make perl>" instead.
|
|---|
| 76 |
|
|---|
| 77 | If you have generated such a statically-linked executable by choice, then
|
|---|
| 78 | instead of saying "C<make test>", you should say "C<make test_static>".
|
|---|
| 79 | On systems that cannot build dynamically-loadable libraries at all, simply
|
|---|
| 80 | saying "C<make test>" is sufficient.
|
|---|
| 81 |
|
|---|
| 82 | =head1 TUTORIAL
|
|---|
| 83 |
|
|---|
| 84 | Now let's go on with the show!
|
|---|
| 85 |
|
|---|
| 86 | =head2 EXAMPLE 1
|
|---|
| 87 |
|
|---|
| 88 | Our first extension will be very simple. When we call the routine in the
|
|---|
| 89 | extension, it will print out a well-known message and return.
|
|---|
| 90 |
|
|---|
| 91 | Run "C<h2xs -A -n Mytest>". This creates a directory named Mytest,
|
|---|
| 92 | possibly under ext/ if that directory exists in the current working
|
|---|
| 93 | directory. Several files will be created in the Mytest dir, including
|
|---|
| 94 | MANIFEST, Makefile.PL, Mytest.pm, Mytest.xs, test.pl, and Changes.
|
|---|
| 95 |
|
|---|
| 96 | The MANIFEST file contains the names of all the files just created in the
|
|---|
| 97 | Mytest directory.
|
|---|
| 98 |
|
|---|
| 99 | The file Makefile.PL should look something like this:
|
|---|
| 100 |
|
|---|
| 101 | use ExtUtils::MakeMaker;
|
|---|
| 102 | # See lib/ExtUtils/MakeMaker.pm for details of how to influence
|
|---|
| 103 | # the contents of the Makefile that is written.
|
|---|
| 104 | WriteMakefile(
|
|---|
| 105 | NAME => 'Mytest',
|
|---|
| 106 | VERSION_FROM => 'Mytest.pm', # finds $VERSION
|
|---|
| 107 | LIBS => [''], # e.g., '-lm'
|
|---|
| 108 | DEFINE => '', # e.g., '-DHAVE_SOMETHING'
|
|---|
| 109 | INC => '', # e.g., '-I/usr/include/other'
|
|---|
| 110 | );
|
|---|
| 111 |
|
|---|
| 112 | The file Mytest.pm should start with something like this:
|
|---|
| 113 |
|
|---|
| 114 | package Mytest;
|
|---|
| 115 |
|
|---|
| 116 | use strict;
|
|---|
| 117 | use warnings;
|
|---|
| 118 |
|
|---|
| 119 | require Exporter;
|
|---|
| 120 | require DynaLoader;
|
|---|
| 121 |
|
|---|
| 122 | our @ISA = qw(Exporter DynaLoader);
|
|---|
| 123 | # Items to export into callers namespace by default. Note: do not export
|
|---|
| 124 | # names by default without a very good reason. Use EXPORT_OK instead.
|
|---|
| 125 | # Do not simply export all your public functions/methods/constants.
|
|---|
| 126 | our @EXPORT = qw(
|
|---|
| 127 |
|
|---|
| 128 | );
|
|---|
| 129 | our $VERSION = '0.01';
|
|---|
| 130 |
|
|---|
| 131 | bootstrap Mytest $VERSION;
|
|---|
| 132 |
|
|---|
| 133 | # Preloaded methods go here.
|
|---|
| 134 |
|
|---|
| 135 | # Autoload methods go after __END__, and are processed by the autosplit program.
|
|---|
| 136 |
|
|---|
| 137 | 1;
|
|---|
| 138 | __END__
|
|---|
| 139 | # Below is the stub of documentation for your module. You better edit it!
|
|---|
| 140 |
|
|---|
| 141 | The rest of the .pm file contains sample code for providing documentation for
|
|---|
| 142 | the extension.
|
|---|
| 143 |
|
|---|
| 144 | Finally, the Mytest.xs file should look something like this:
|
|---|
| 145 |
|
|---|
| 146 | #include "EXTERN.h"
|
|---|
| 147 | #include "perl.h"
|
|---|
| 148 | #include "XSUB.h"
|
|---|
| 149 |
|
|---|
| 150 | MODULE = Mytest PACKAGE = Mytest
|
|---|
| 151 |
|
|---|
| 152 | Let's edit the .xs file by adding this to the end of the file:
|
|---|
| 153 |
|
|---|
| 154 | void
|
|---|
| 155 | hello()
|
|---|
| 156 | CODE:
|
|---|
| 157 | printf("Hello, world!\n");
|
|---|
| 158 |
|
|---|
| 159 | It is okay for the lines starting at the "CODE:" line to not be indented.
|
|---|
| 160 | However, for readability purposes, it is suggested that you indent CODE:
|
|---|
| 161 | one level and the lines following one more level.
|
|---|
| 162 |
|
|---|
| 163 | Now we'll run "C<perl Makefile.PL>". This will create a real Makefile,
|
|---|
| 164 | which make needs. Its output looks something like:
|
|---|
| 165 |
|
|---|
| 166 | % perl Makefile.PL
|
|---|
| 167 | Checking if your kit is complete...
|
|---|
| 168 | Looks good
|
|---|
| 169 | Writing Makefile for Mytest
|
|---|
| 170 | %
|
|---|
| 171 |
|
|---|
| 172 | Now, running make will produce output that looks something like this (some
|
|---|
| 173 | long lines have been shortened for clarity and some extraneous lines have
|
|---|
| 174 | been deleted):
|
|---|
| 175 |
|
|---|
| 176 | % make
|
|---|
| 177 | umask 0 && cp Mytest.pm ./blib/Mytest.pm
|
|---|
| 178 | perl xsubpp -typemap typemap Mytest.xs >Mytest.tc && mv Mytest.tc Mytest.c
|
|---|
| 179 | Please specify prototyping behavior for Mytest.xs (see perlxs manual)
|
|---|
| 180 | cc -c Mytest.c
|
|---|
| 181 | Running Mkbootstrap for Mytest ()
|
|---|
| 182 | chmod 644 Mytest.bs
|
|---|
| 183 | LD_RUN_PATH="" ld -o ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl -b Mytest.o
|
|---|
| 184 | chmod 755 ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl
|
|---|
| 185 | cp Mytest.bs ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs
|
|---|
| 186 | chmod 644 ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs
|
|---|
| 187 | Manifying ./blib/man3/Mytest.3
|
|---|
| 188 | %
|
|---|
| 189 |
|
|---|
| 190 | You can safely ignore the line about "prototyping behavior" - it is
|
|---|
| 191 | explained in the section "The PROTOTYPES: Keyword" in L<perlxs>.
|
|---|
| 192 |
|
|---|
| 193 | If you are on a Win32 system, and the build process fails with linker
|
|---|
| 194 | errors for functions in the C library, check if your Perl is configured
|
|---|
| 195 | to use PerlCRT (running B<perl -V:libc> should show you if this is the
|
|---|
| 196 | case). If Perl is configured to use PerlCRT, you have to make sure
|
|---|
| 197 | PerlCRT.lib is copied to the same location that msvcrt.lib lives in,
|
|---|
| 198 | so that the compiler can find it on its own. msvcrt.lib is usually
|
|---|
| 199 | found in the Visual C compiler's lib directory (e.g. C:/DevStudio/VC/lib).
|
|---|
| 200 |
|
|---|
| 201 | Perl has its own special way of easily writing test scripts, but for this
|
|---|
| 202 | example only, we'll create our own test script. Create a file called hello
|
|---|
| 203 | that looks like this:
|
|---|
| 204 |
|
|---|
| 205 | #! /opt/perl5/bin/perl
|
|---|
| 206 |
|
|---|
| 207 | use ExtUtils::testlib;
|
|---|
| 208 |
|
|---|
| 209 | use Mytest;
|
|---|
| 210 |
|
|---|
| 211 | Mytest::hello();
|
|---|
| 212 |
|
|---|
| 213 | Now we make the script executable (C<chmod +x hello>), run the script
|
|---|
| 214 | and we should see the following output:
|
|---|
| 215 |
|
|---|
| 216 | % ./hello
|
|---|
| 217 | Hello, world!
|
|---|
| 218 | %
|
|---|
| 219 |
|
|---|
| 220 | =head2 EXAMPLE 2
|
|---|
| 221 |
|
|---|
| 222 | Now let's add to our extension a subroutine that will take a single numeric
|
|---|
| 223 | argument as input and return 0 if the number is even or 1 if the number
|
|---|
| 224 | is odd.
|
|---|
| 225 |
|
|---|
| 226 | Add the following to the end of Mytest.xs:
|
|---|
| 227 |
|
|---|
| 228 | int
|
|---|
| 229 | is_even(input)
|
|---|
| 230 | int input
|
|---|
| 231 | CODE:
|
|---|
| 232 | RETVAL = (input % 2 == 0);
|
|---|
| 233 | OUTPUT:
|
|---|
| 234 | RETVAL
|
|---|
| 235 |
|
|---|
| 236 | There does not need to be whitespace at the start of the "C<int input>"
|
|---|
| 237 | line, but it is useful for improving readability. Placing a semi-colon at
|
|---|
| 238 | the end of that line is also optional. Any amount and kind of whitespace
|
|---|
| 239 | may be placed between the "C<int>" and "C<input>".
|
|---|
| 240 |
|
|---|
|
|---|