source: trunk/essentials/dev-lang/perl/pod/perlxstut.pod@ 3609

Last change on this file since 3609 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 48.9 KB
Line 
1=head1 NAME
2
3perlXStut - Tutorial for writing XSUBs
4
5=head1 DESCRIPTION
6
7This tutorial will educate the reader on the steps involved in creating
8a Perl extension. The reader is assumed to have access to L<perlguts>,
9L<perlapi> and L<perlxs>.
10
11This tutorial starts with very simple examples and becomes more complex,
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.
15
16This tutorial was written from a Unix point of view. Where I know them
17to be otherwise different for other platforms (e.g. Win32), I will list
18them. If you find something that was missed, please let me know.
19
20=head1 SPECIAL NOTES
21
22=head2 make
23
24This tutorial assumes that the make program that Perl is configured to
25use is called C<make>. Instead of running "make" in the examples that
26follow, you may have to substitute whatever make program Perl has been
27configured to use. Running B<perl -V:make> should tell you what it is.
28
29=head2 Version caveat
30
31When writing a Perl extension for general consumption, one should expect that
32the extension will be used with versions of Perl different from the
33version available on your machine. Since you are reading this document,
34the version of Perl on your machine is probably 5.005 or later, but the users
35of your extension may have more ancient versions.
36
37To understand what kinds of incompatibilities one may expect, and in the rare
38case that the version of Perl on your machine is older than this document,
39see the section on "Troubleshooting these Examples" for more information.
40
41If your extension uses some features of Perl which are not available on older
42releases of Perl, your users would appreciate an early meaningful warning.
43You would probably put this information into the F<README> file, but nowadays
44installation of extensions may be performed automatically, guided by F<CPAN.pm>
45module or other tools.
46
47In MakeMaker-based installations, F<Makefile.PL> provides the earliest
48opportunity to perform version checks. One can put something like this
49in 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
61It is commonly thought that if a system does not have the capability to
62dynamically load a library, you cannot build XSUBs. This is incorrect.
63You I<can> build them, but you must link the XSUBs subroutines with the
64rest of Perl, creating a new executable. This situation is similar to
65Perl 4.
66
67This tutorial can still be used on such a system. The XSUB build mechanism
68will check the system and build a dynamically-loadable library if possible,
69or else a static library and then, optionally, a new statically-linked
70executable with that static library linked in.
71
72Should you wish to build a statically-linked executable on a system which
73can dynamically load libraries, you may, in all the following examples,
74where the command "C<make>" with no arguments is executed, run the command
75"C<make perl>" instead.
76
77If you have generated such a statically-linked executable by choice, then
78instead of saying "C<make test>", you should say "C<make test_static>".
79On systems that cannot build dynamically-loadable libraries at all, simply
80saying "C<make test>" is sufficient.
81
82=head1 TUTORIAL
83
84Now let's go on with the show!
85
86=head2 EXAMPLE 1
87
88Our first extension will be very simple. When we call the routine in the
89extension, it will print out a well-known message and return.
90
91Run "C<h2xs -A -n Mytest>". This creates a directory named Mytest,
92possibly under ext/ if that directory exists in the current working
93directory. Several files will be created in the Mytest dir, including
94MANIFEST, Makefile.PL, Mytest.pm, Mytest.xs, test.pl, and Changes.
95
96The MANIFEST file contains the names of all the files just created in the
97Mytest directory.
98
99The 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
112The 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
141The rest of the .pm file contains sample code for providing documentation for
142the extension.
143
144Finally, 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
152Let'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
159It is okay for the lines starting at the "CODE:" line to not be indented.
160However, for readability purposes, it is suggested that you indent CODE:
161one level and the lines following one more level.
162
163Now we'll run "C<perl Makefile.PL>". This will create a real Makefile,
164which 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
172Now, running make will produce output that looks something like this (some
173long lines have been shortened for clarity and some extraneous lines have
174been 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
190You can safely ignore the line about "prototyping behavior" - it is
191explained in the section "The PROTOTYPES: Keyword" in L<perlxs>.
192
193If you are on a Win32 system, and the build process fails with linker
194errors for functions in the C library, check if your Perl is configured
195to use PerlCRT (running B<perl -V:libc> should show you if this is the
196case). If Perl is configured to use PerlCRT, you have to make sure
197PerlCRT.lib is copied to the same location that msvcrt.lib lives in,
198so that the compiler can find it on its own. msvcrt.lib is usually
199found in the Visual C compiler's lib directory (e.g. C:/DevStudio/VC/lib).
200
201Perl has its own special way of easily writing test scripts, but for this
202example only, we'll create our own test script. Create a file called hello
203that looks like this:
204
205 #! /opt/perl5/bin/perl
206
207 use ExtUtils::testlib;
208
209 use Mytest;
210
211 Mytest::hello();
212
213Now we make the script executable (C<chmod +x hello>), run the script
214and we should see the following output:
215
216 % ./hello
217 Hello, world!
218 %
219
220=head2 EXAMPLE 2
221
222Now let's add to our extension a subroutine that will take a single numeric
223argument as input and return 0 if the number is even or 1 if the number
224is odd.
225
226Add 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
236There does not need to be whitespace at the start of the "C<int input>"
237line, but it is useful for improving readability. Placing a semi-colon at
238the end of that line is also optional. Any amount and kind of whitespace
239may be placed between the "C<int>" and "C<input>".
240