support for list assignment to pseudohashes (from John Tobey
[p5sagit/p5-mst-13.2.git] / pod / perlxstut.pod
CommitLineData
4633a7c4 1=head1 NAME
2
360e660c 3perlXStut - Tutorial for writing XSUBs
4633a7c4 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
360e660c 13completely explained until later in the tutorial in order to slowly ease
14the reader into building extensions.
4633a7c4 15
360e660c 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.
4633a7c4 19
360e660c 20=head1 SPECIAL NOTES
c07a80fd 21
360e660c 22=head2 make
c07a80fd 23
360e660c 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
5a3e7812 27configured to use. Running B<perl -V:make> should tell you what it is.
c07a80fd 28
360e660c 29=head2 Version caveat
c07a80fd 30
beb31b0b 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
c07a80fd 58
360e660c 59=head2 Dynamic Loading versus Static Loading
c07a80fd 60
61It is commonly thought that if a system does not have the capability to
360e660c 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
c07a80fd 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,
360e660c 74where the command "C<make>" with no arguments is executed, run the command
75"C<make perl>" instead.
c07a80fd 76
77If you have generated such a statically-linked executable by choice, then
360e660c 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!
c07a80fd 85
86=head2 EXAMPLE 1
4633a7c4 87
88Our first extension will be very simple. When we call the routine in the
c07a80fd 89extension, it will print out a well-known message and return.
4633a7c4 90
360e660c 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.
4633a7c4 95
360e660c 96The MANIFEST file contains the names of all the files just created in the
97Mytest directory.
4633a7c4 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(
360e660c 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'
4633a7c4 110 );
111
791fa977 112The file Mytest.pm should start with something like this:
c07a80fd 113
791fa977 114 package Mytest;
4633a7c4 115
360e660c 116 use strict;
360e660c 117
4633a7c4 118 require Exporter;
119 require DynaLoader;
c07a80fd 120
77ca0c92 121 our @ISA = qw(Exporter DynaLoader);
4633a7c4 122 # Items to export into callers namespace by default. Note: do not export
123 # names by default without a very good reason. Use EXPORT_OK instead.
124 # Do not simply export all your public functions/methods/constants.
77ca0c92 125 our @EXPORT = qw(
c07a80fd 126
4633a7c4 127 );
77ca0c92 128 our $VERSION = '0.01';
c07a80fd 129
791fa977 130 bootstrap Mytest $VERSION;
c07a80fd 131
4633a7c4 132 # Preloaded methods go here.
c07a80fd 133
4633a7c4 134 # Autoload methods go after __END__, and are processed by the autosplit program.
c07a80fd 135
4633a7c4 136 1;
137 __END__
c07a80fd 138 # Below is the stub of documentation for your module. You better edit it!
4633a7c4 139
360e660c 140The rest of the .pm file contains sample code for providing documentation for
141the extension.
142
143Finally, the Mytest.xs file should look something like this:
4633a7c4 144
145 #include "EXTERN.h"
146 #include "perl.h"
147 #include "XSUB.h"
791fa977 148
149 MODULE = Mytest PACKAGE = Mytest
4633a7c4 150
151Let's edit the .xs file by adding this to the end of the file:
152
153 void
154 hello()
360e660c 155 CODE:
4633a7c4 156 printf("Hello, world!\n");
157
360e660c 158It is okay for the lines starting at the "CODE:" line to not be indented.
159However, for readability purposes, it is suggested that you indent CODE:
160one level and the lines following one more level.
161
162Now we'll run "C<perl Makefile.PL>". This will create a real Makefile,
d9d2a7fb 163which make needs. Its output looks something like:
4633a7c4 164
165 % perl Makefile.PL
166 Checking if your kit is complete...
167 Looks good
791fa977 168 Writing Makefile for Mytest
4633a7c4 169 %
170
360e660c 171Now, running make will produce output that looks something like this (some
172long lines have been shortened for clarity and some extraneous lines have
173been deleted):
4633a7c4 174
175 % make
791fa977 176 umask 0 && cp Mytest.pm ./blib/Mytest.pm
177 perl xsubpp -typemap typemap Mytest.xs >Mytest.tc && mv Mytest.tc Mytest.c
360e660c 178 Please specify prototyping behavior for Mytest.xs (see perlxs manual)
791fa977 179 cc -c Mytest.c
180 Running Mkbootstrap for Mytest ()
181 chmod 644 Mytest.bs
182 LD_RUN_PATH="" ld -o ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl -b Mytest.o
183 chmod 755 ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl
184 cp Mytest.bs ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs
185 chmod 644 ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs
360e660c 186 Manifying ./blib/man3/Mytest.3
187 %
188
189You can safely ignore the line about "prototyping behavior".
190
191If you are on a Win32 system, and the build process fails with linker
192errors for functions in the C library, check if your Perl is configured
5a3e7812 193to use PerlCRT (running B<perl -V:libc> should show you if this is the
360e660c 194case). If Perl is configured to use PerlCRT, you have to make sure
195PerlCRT.lib is copied to the same location that msvcrt.lib lives in,
196so that the compiler can find it on its own. msvcrt.lib is usually
197found in the Visual C compiler's lib directory (e.g. C:/DevStudio/VC/lib).
c07a80fd 198
360e660c 199Perl has its own special way of easily writing test scripts, but for this
200example only, we'll create our own test script. Create a file called hello
c07a80fd 201that looks like this:
202
c07a80fd 203 #! /opt/perl5/bin/perl
360e660c 204
d9d2a7fb 205 use ExtUtils::testlib;
360e660c 206
791fa977 207 use Mytest;
360e660c 208
791fa977 209 Mytest::hello();
4633a7c4 210
360e660c 211Now we make the script executable (C<chmod -x hello>), run the script
212and we should see the following output:
4633a7c4 213
360e660c 214 % ./hello
4633a7c4 215 Hello, world!
216 %
217
c07a80fd 218=head2 EXAMPLE 2
4633a7c4 219
360e660c 220Now let's add to our extension a subroutine that will take a single numeric
221argument as input and return 0 if the number is even or 1 if the number
222is odd.
4633a7c4 223
791fa977 224Add the following to the end of Mytest.xs:
4633a7c4 225
226 int
227 is_even(input)
228 int input
360e660c 229 CODE:
c07a80fd 230 RETVAL = (input % 2 == 0);
360e660c 231 OUTPUT:
4633a7c4 232 RETVAL
233
360e660c 234There does not need to be white space at the start of the "C<int input>"
235line, but it is useful for improving readability. Placing a semi-colon at
236the end of that line is also optional. Any amount and kind of white space
237may be placed between the "C<int>" and "C<input>".
4633a7c4 238
360e660c 239Now re-run make to rebuild our new shared library.
4633a7c4 240
c07a80fd 241Now perform the same steps as before, generating a Makefile from the
242Makefile.PL file, and running make.
4633a7c4 243
360e660c 244In order to test that our extension works, we now need to look at the
c07a80fd 245file test.pl. This file is set up to imitate the same kind of testing
246structure that Perl itself has. Within the test script, you perform a
247number of tests to confirm the behavior of the extension, printing "ok"
d9d2a7fb 248when the test is correct, "not ok" when it is not. Change the print
249statement in the BEGIN block to print "1..4", and add the following code
250to the end of the file:
c07a80fd 251
791fa977 252 print &Mytest::is_even(0) == 1 ? "ok 2" : "not ok 2", "\n";
253 print &Mytest::is_even(1) == 0 ? "ok 3" : "not ok 3", "\n";
254 print &Mytest::is_even(2) == 1 ? "ok 4" : "not ok 4", "\n";
c07a80fd 255
360e660c 256We will be calling the test script through the command "C<make test>". You
c07a80fd 257should see output that looks something like this:
258
259 % make test
360e660c 260 PERL_DL_NONLAZY=1 /opt/perl5.004/bin/perl (lots of -I arguments) test.pl
c07a80fd 261 1..4
262 ok 1
263 ok 2
264 ok 3
265 ok 4
4633a7c4 266 %
267
360e660c 268=head2 What has gone on?
4633a7c4 269
270The program h2xs is the starting point for creating extensions. In later
c07a80fd 271examples we'll see how we can use h2xs to read header files and generate
4633a7c4 272templates to connect to C routines.
273
274h2xs creates a number of files in the extension directory. The file
275Makefile.PL is a perl script which will generate a true Makefile to build
276the extension. We'll take a closer look at it later.
277
360e660c 278The .pm and .xs files contain the meat of the extension. The .xs file holds
279the C routines that make up the extension. The .pm file contains routines
280that tell Perl how to load your extension.
281
282Generating the Makefile and running C<make> created a directory called blib
283(which stands for "build library") in the current working directory. This
284directory will contain the shared library that we will build. Once we have
285tested it, we can install it into its final location.
286
287Invoking the test script via "C<make test>" did something very important.
288It invoked perl with all those C<-I> arguments so that it could find the
289various files that are part of the extension. It is I<very> important that
290while you are still testing extensions that you use "C<make test>". If you
291try to run the test script all by itself, you will get a fatal error.
292Another reason it is important to use "C<make test>" to run your test
293script is that if you are testing an upgrade to an already-existing version,
294using "C<make test>" insures that you will test your new extension, not the
295already-existing version.
4633a7c4 296
c07a80fd 297When Perl sees a C<use extension;>, it searches for a file with the same name
360e660c 298as the C<use>'d extension that has a .pm suffix. If that file cannot be found,
c07a80fd 299Perl dies with a fatal error. The default search path is contained in the
360e660c 300C<@INC> array.
4633a7c4 301
791fa977 302In our case, Mytest.pm tells perl that it will need the Exporter and Dynamic
360e660c 303Loader extensions. It then sets the C<@ISA> and C<@EXPORT> arrays and the
304C<$VERSION> scalar; finally it tells perl to bootstrap the module. Perl
305will call its dynamic loader routine (if there is one) and load the shared
306library.
4633a7c4 307
360e660c 308The two arrays C<@ISA> and C<@EXPORT> are very important. The C<@ISA>
c07a80fd 309array contains a list of other packages in which to search for methods (or
360e660c 310subroutines) that do not exist in the current package. This is usually
311only important for object-oriented extensions (which we will talk about
312much later), and so usually doesn't need to be modified.
4633a7c4 313
360e660c 314The C<@EXPORT> array tells Perl which of the extension's variables and
315subroutines should be placed into the calling package's namespace. Because
316you don't know if the user has already used your variable and subroutine
317names, it's vitally important to carefully select what to export. Do I<not>
318export method or variable names I<by default> without a good reason.
4633a7c4 319
c07a80fd 320As a general rule, if the module is trying to be object-oriented then don't
360e660c 321export anything. If it's just a collection of functions and variables, then
322you can export them via another array, called C<@EXPORT_OK>. This array
323does not automatically place its subroutine and variable names into the
324namespace unless the user specifically requests that this be done.
4633a7c4 325
c07a80fd 326See L<perlmod> for more information.
4633a7c4 327
360e660c 328The C<$VERSION> variable is used to ensure that the .pm file and the shared
791fa977 329library are "in sync" with each other. Any time you make changes to
330the .pm or .xs files, you should increment the value of this variable.
331
360e660c 332=head2 Writing good test scripts
791fa977 333
334The importance of writing good test scripts cannot be overemphasized. You
335should closely follow the "ok/not ok" style that Perl itself uses, so that
336it is very easy and unambiguous to determine the outcome of each test case.
337When you find and fix a bug, make sure you add a test case for it.
338
360e660c 339By running "C<make test>", you ensure that your test.pl script runs and uses
791fa977 340the correct version of your extension. If you have many test cases, you
360e660c 341might want to copy Perl's test style. Create a directory named "t" in the
342extension's directory and append the suffix ".t" to the names of your test
343files. When you run "C<make test>", all of these test files will be executed.
4633a7c4 344
c07a80fd 345=head2 EXAMPLE 3
4633a7c4 346
347Our third extension will take one argument as its input, round off that
c07a80fd 348value, and set the I<argument> to the rounded value.
4633a7c4 349
791fa977 350Add the following to the end of Mytest.xs:
4633a7c4 351
352 void
353 round(arg)
354 double arg
360e660c 355 CODE:
4633a7c4 356 if (arg > 0.0) {
357 arg = floor(arg + 0.5);
358 } else if (arg < 0.0) {
359 arg = ceil(arg - 0.5);
360 } else {
361 arg = 0.0;
362 }
360e660c 363 OUTPUT:
4633a7c4 364 arg
365
c07a80fd 366Edit the Makefile.PL file so that the corresponding line looks like this:
4633a7c4 367
368 'LIBS' => ['-lm'], # e.g., '-lm'
369
360e660c 370Generate the Makefile and run make. Change the BEGIN block to print
c07a80fd 371"1..9" and add the following to test.pl:
4633a7c4 372
791fa977 373 $i = -1.5; &Mytest::round($i); print $i == -2.0 ? "ok 5" : "not ok 5", "\n";
374 $i = -1.1; &Mytest::round($i); print $i == -1.0 ? "ok 6" : "not ok 6", "\n";
375 $i = 0.0; &Mytest::round($i); print $i == 0.0 ? "ok 7" : "not ok 7", "\n";
376 $i = 0.5; &Mytest::round($i); print $i == 1.0 ? "ok 8" : "not ok 8", "\n";
377 $i = 1.2; &Mytest::round($i); print $i == 1.0 ? "ok 9" : "not ok 9", "\n";
c07a80fd 378
360e660c 379Running "C<make test>" should now print out that all nine tests are okay.
4633a7c4 380
360e660c 381Notice that in these new test cases, the argument passed to round was a
382scalar variable. You might be wondering if you can round a constant or
383literal. To see what happens, temporarily add the following line to test.pl:
4633a7c4 384
791fa977 385 &Mytest::round(3);
4633a7c4 386
360e660c 387Run "C<make test>" and notice that Perl dies with a fatal error. Perl won't
388let you change the value of constants!
4633a7c4 389
360e660c 390=head2 What's new here?
4633a7c4 391
360e660c 392=over 4
4633a7c4 393
360e660c 394=item *
4633a7c4 395
360e660c 396We've made some changes to Makefile.PL. In this case, we've specified an
397extra library to be linked into the extension's shared library, the math
398library libm in this case. We'll talk later about how to write XSUBs that
399can call every routine in a library.
4633a7c4 400
360e660c 401=item *
402
403The value of the function is not being passed back as the function's return
404value, but by changing the value of the variable that was passed into the
405function. You might have guessed that when you saw that the return value
406of round is of type "void".
407
408=back
409
410=head2 Input and Output Parameters
4633a7c4 411
360e660c 412You specify the parameters that will be passed into the XSUB on the line(s)
413after you declare the function's return value and name. Each input parameter
414line starts with optional white space, and may have an optional terminating
415semicolon.
4633a7c4 416
360e660c 417The list of output parameters occurs at the very end of the function, just
418before after the OUTPUT: directive. The use of RETVAL tells Perl that you
419wish to send this value back as the return value of the XSUB function. In
420Example 3, we wanted the "return value" placed in the original variable
421which we passed in, so we listed it (and not RETVAL) in the OUTPUT: section.
4633a7c4 422
360e660c 423=head2 The XSUBPP Program
424
beb31b0b 425The B<xsubpp> program takes the XS code in the .xs file and translates it into
4633a7c4 426C code, placing it in a file whose suffix is .c. The C code created makes
427heavy use of the C functions within Perl.
428
360e660c 429=head2 The TYPEMAP file
4633a7c4 430
beb31b0b 431The B<xsubpp> program uses rules to convert from Perl's data types (scalar,
360e660c 432array, etc.) to C's data types (int, char, etc.). These rules are stored
4633a7c4 433in the typemap file ($PERLLIB/ExtUtils/typemap). This file is split into
434three parts.
435
360e660c 436The first section maps various C data types to a name, which corresponds
437somewhat with the various Perl types. The second section contains C code
beb31b0b 438which B<xsubpp> uses to handle input parameters. The third section contains
439C code which B<xsubpp> uses to handle output parameters.
4633a7c4 440
360e660c 441Let's take a look at a portion of the .c file created for our extension.
442The file name is Mytest.c:
4633a7c4 443
791fa977 444 XS(XS_Mytest_round)
4633a7c4 445 {
446 dXSARGS;
c07a80fd 447 if (items != 1)
791fa977 448 croak("Usage: Mytest::round(arg)");
4633a7c4 449 {
c07a80fd 450 double arg = (double)SvNV(ST(0)); /* XXXXX */
4633a7c4 451 if (arg > 0.0) {
452 arg = floor(arg + 0.5);
453 } else if (arg < 0.0) {
454 arg = ceil(arg - 0.5);
c07a80fd 455 } else {
456 arg = 0.0;
4633a7c4 457 }
360e660c 458 sv_setnv(ST(0), (double)arg); /* XXXXX */
4633a7c4 459 }
460 XSRETURN(1);
461 }
4633a7c4 462
360e660c 463Notice the two lines commented with "XXXXX". If you check the first section
464of the typemap file, you'll see that doubles are of type T_DOUBLE. In the
4633a7c4 465INPUT section, an argument that is T_DOUBLE is assigned to the variable
466arg by calling the routine SvNV on something, then casting it to double,
467then assigned to the variable arg. Similarly, in the OUTPUT section,
ef50df4b 468once arg has its final value, it is passed to the sv_setnv function to
469be passed back to the calling subroutine. These two functions are explained
470in L<perlguts>; we'll talk more later about what that "ST(0)" means in the
471section on the argument stack.
4633a7c4 472
360e660c 473=head2 Warning about Output Arguments
4633a7c4 474
c07a80fd 475In general, it's not a good idea to write extensions that modify their input
360e660c 476parameters, as in Example 3. Instead, you should probably return multiple
477values in an array and let the caller handle them (we'll do this in a later
478example). However, in order to better accomodate calling pre-existing C
479routines, which often do modify their input parameters, this behavior is
480tolerated.
791fa977 481
482=head2 EXAMPLE 4
483
68dc0745 484In this example, we'll now begin to write XSUBs that will interact with
360e660c 485pre-defined C libraries. To begin with, we will build a small library of
791fa977 486our own, then let h2xs write our .pm and .xs files for us.
487
488Create a new directory called Mytest2 at the same level as the directory
489Mytest. In the Mytest2 directory, create another directory called mylib,
490and cd into that directory.
491
492Here we'll create some files that will generate a test library. These will
493include a C source file and a header file. We'll also create a Makefile.PL
494in this directory. Then we'll make sure that running make at the Mytest2
495level will automatically run this Makefile.PL file and the resulting Makefile.
496
9693b09d 497In the mylib directory, create a file mylib.h that looks like this:
791fa977 498
499 #define TESTVAL 4
500
501 extern double foo(int, long, const char*);
502
503Also create a file mylib.c that looks like this:
504
505 #include <stdlib.h>
506 #include "./mylib.h"
360e660c 507
791fa977 508 double
360e660c 509 foo(int a, long b, const char *c)
791fa977 510 {
511 return (a + b + atof(c) + TESTVAL);
512 }
513
514And finally create a file Makefile.PL that looks like this:
515
516 use ExtUtils::MakeMaker;
517 $Verbose = 1;
518 WriteMakefile(
360e660c 519 NAME => 'Mytest2::mylib',
520 SKIP => [qw(all static static_lib dynamic dynamic_lib)],
521 clean => {'FILES' => 'libmylib$(LIBEEXT)'},
791fa977 522 );
523
524
8227f81c 525 sub MY::top_targets {
791fa977 526 '
527 all :: static
528
360e660c 529 pure_all :: static
530
791fa977 531 static :: libmylib$(LIB_EXT)
532
533 libmylib$(LIB_EXT): $(O_FILES)
534 $(AR) cr libmylib$(LIB_EXT) $(O_FILES)
535 $(RANLIB) libmylib$(LIB_EXT)
536
537 ';
538 }
539
360e660c 540Make sure you use a tab and not spaces on the lines beginning with "$(AR)"
541and "$(RANLIB)". Make will not function properly if you use spaces.
542It has also been reported that the "cr" argument to $(AR) is unnecessary
543on Win32 systems.
544
791fa977 545We will now create the main top-level Mytest2 files. Change to the directory
546above Mytest2 and run the following command:
547
d9d2a7fb 548 % h2xs -O -n Mytest2 ./Mytest2/mylib/mylib.h
791fa977 549
550This will print out a warning about overwriting Mytest2, but that's okay.
551Our files are stored in Mytest2/mylib, and will be untouched.
552
553The normal Makefile.PL that h2xs generates doesn't know about the mylib
554directory. We need to tell it that there is a subdirectory and that we
360e660c 555will be generating a library in it. Let's add the argument MYEXTLIB to
556the WriteMakefile call so that it looks like this:
4633a7c4 557
360e660c 558 WriteMakefile(
559 'NAME' => 'Mytest2',
560 'VERSION_FROM' => 'Mytest2.pm', # finds $VERSION
561 'LIBS' => [''], # e.g., '-lm'
562 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING'
563 'INC' => '', # e.g., '-I/usr/include/other'
564 'MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)',
565 );
791fa977 566
360e660c 567and then at the end add a subroutine (which will override the pre-existing
568subroutine). Remember to use a tab character to indent the line beginning
569with "cd"!
791fa977 570
571 sub MY::postamble {
572 '
573 $(MYEXTLIB): mylib/Makefile
360e660c 574 cd mylib && $(MAKE) $(PASSTHRU)
791fa977 575 ';
576 }
577
791fa977 578Let's also fix the MANIFEST file so that it accurately reflects the contents
579of our extension. The single line that says "mylib" should be replaced by
580the following three lines:
581
582 mylib/Makefile.PL
583 mylib/mylib.c
584 mylib/mylib.h
585
586To keep our namespace nice and unpolluted, edit the .pm file and change
77ca0c92 587the variable C<@EXPORT> to C<@EXPORT_OK>. Finally, in the
d9d2a7fb 588.xs file, edit the #include line to read:
791fa977 589
590 #include "mylib/mylib.h"
591
592And also add the following function definition to the end of the .xs file:
593
594 double
595 foo(a,b,c)
596 int a
597 long b
598 const char * c
360e660c 599 OUTPUT:
791fa977 600 RETVAL
601
602Now we also need to create a typemap file because the default Perl doesn't
360e660c 603currently support the const char * type. Create a file called typemap in
604the Mytest2 directory and place the following in it:
791fa977 605
606 const char * T_PV
607
608Now run perl on the top-level Makefile.PL. Notice that it also created a
360e660c 609Makefile in the mylib directory. Run make and watch that it does cd into
791fa977 610the mylib directory and run make in there as well.
611
612Now edit the test.pl script and change the BEGIN block to print "1..4",
613and add the following lines to the end of the script:
614
615 print &Mytest2::foo(1, 2, "Hello, world!") == 7 ? "ok 2\n" : "not ok 2\n";
616 print &Mytest2::foo(1, 2, "0.0") == 7 ? "ok 3\n" : "not ok 3\n";
617 print abs(&Mytest2::foo(0, 0, "-3.4") - 0.6) <= 0.01 ? "ok 4\n" : "not ok 4\n";
618
360e660c 619(When dealing with floating-point comparisons, it is best to not check for
620equality, but rather that the difference between the expected and actual
621result is below a certain amount (called epsilon) which is 0.01 in this case)
791fa977 622
360e660c 623Run "C<make test>" and all should be well.
791fa977 624
360e660c 625=head2 What has happened here?
791fa977 626
627Unlike previous examples, we've now run h2xs on a real include file. This
628has caused some extra goodies to appear in both the .pm and .xs files.
629
84dc3c4d 630=over 4
631
791fa977 632=item *
633
360e660c 634In the .xs file, there's now a #include directive with the absolute path to
635the mylib.h header file. We changed this to a relative path so that we
636could move the extension directory if we wanted to.
791fa977 637
638=item *
639
640There's now some new C code that's been added to the .xs file. The purpose
641of the C<constant> routine is to make the values that are #define'd in the
360e660c 642header file accessible by the Perl script (by calling either C<TESTVAL> or
643C<&Mytest2::TESTVAL>). There's also some XS code to allow calls to the
791fa977 644C<constant> routine.
645
646=item *
647
360e660c 648The .pm file originally exported the name C<TESTVAL> in the C<@EXPORT> array.
649This could lead to name clashes. A good rule of thumb is that if the #define
650is only going to be used by the C routines themselves, and not by the user,
651they should be removed from the C<@EXPORT> array. Alternately, if you don't
652mind using the "fully qualified name" of a variable, you could move most
653or all of the items from the C<@EXPORT> array into the C<@EXPORT_OK> array.
791fa977 654
d9d2a7fb 655=item *
656
360e660c 657If our include file had contained #include directives, these would not have
658been processed by h2xs. There is no good solution to this right now.
d9d2a7fb 659
360e660c 660=item *
791fa977 661
662We've also told Perl about the library that we built in the mylib
360e660c 663subdirectory. That required only the addition of the C<MYEXTLIB> variable
791fa977 664to the WriteMakefile call and the replacement of the postamble subroutine
665to cd into the subdirectory and run make. The Makefile.PL for the
666library is a bit more complicated, but not excessively so. Again we
667replaced the postamble subroutine to insert our own code. This code
360e660c 668simply specified that the library to be created here was a static archive
669library (as opposed to a dynamically loadable library) and provided the
791fa977 670commands to build it.
4633a7c4 671
360e660c 672=back
673
beb31b0b 674=head2 Anatomy of .xs file
675
676The .xs file of L<"EXAMPLE 4"> contained some new elements. To understand
677the meaning of these elements, pay attention to the line which reads
678
679 MODULE = Mytest2 PACKAGE = Mytest2
680
681Anything before this line is plain C code which describes which headers
682to include, and defines some convenience functions. No translations are
683performed on this part, it goes into the generated output C file as is.
684
685Anything after this line is the description of XSUB functions.
686These descriptions are translated by B<xsubpp> into C code which
687implements these functions using Perl calling conventions, and which
688makes these functions visible from Perl interpreter.
689
690Pay a special attention to the function C<constant>. This name appears
691twice in the generated .xs file: once in the first part, as a static C
692function, the another time in the second part, when an XSUB interface to
693this static C function is defined.
694
695This is quite typical for .xs files: usually the .xs file provides
696an interface to an existing C function. Then this C function is defined
697somewhere (either in an external library, or in the first part of .xs file),
698and a Perl interface to this function (i.e. "Perl glue") is described in the
699second part of .xs file. The situation in L<"EXAMPLE 1">, L<"EXAMPLE 2">,
700and L<"EXAMPLE 3">, when all the work is done inside the "Perl glue", is
701somewhat of an exception rather than the rule.
702
703=head2 Getting the fat out of XSUBs
704
705In L<"EXAMPLE 4"> the second part of .xs file contained the following
706description of an XSUB:
707
708 double
709 foo(a,b,c)
710 int a
711 long b
712 const char * c
713 OUTPUT:
714 RETVAL
715
716Note that in contrast with L<"EXAMPLE 1">, L<"EXAMPLE 2"> and L<"EXAMPLE 3">,
717this description does not contain the actual I<code> for what is done
718is done during a call to Perl function foo(). To understand what is going
719on here, one can add a CODE section to this XSUB:
720
721 double
722 foo(a,b,c)
723 int a
724 long b
725 const char * c
726 CODE:
727 RETVAL = foo(a,b,c);
728 OUTPUT:
729 RETVAL
730
731However, these two XSUBs provide almost identical generated C code: B<xsubpp>
732compiler is smart enough to figure out the C<CODE:> section from the first
733two lines of the description of XSUB. What about C<OUTPUT:> section? In
734fact, that is absolutely the same! The C<OUTPUT:> section can be removed
735as well, I<as far as C<CODE:> section or C<PPCODE:> section> is not
736specified: B<xsubpp> can see that it needs to generate a function call
737section, and will autogenerate the OUTPUT section too. Thus one can
738shortcut the XSUB to become:
739
740 double
741 foo(a,b,c)
742 int a
743 long b
744 const char * c
745
746Can we do the same with an XSUB
747
748 int
749 is_even(input)
750 int input
751 CODE:
752 RETVAL = (input % 2 == 0);
753 OUTPUT:
754 RETVAL
755
756of L<"EXAMPLE 2">? To do this, one needs to define a C function C<int
757is_even(int input)>. As we saw in L<Anatomy of .xs file>, a proper place
758for this definition is in the first part of .xs file. In fact a C function
759
760 int
761 is_even(int arg)
762 {
763 return (arg % 2 == 0);
764 }
765
766is probably overkill for this. Something as simple as a C<#define> will
767do too:
768
769 #define is_even(arg) ((arg) % 2 == 0)
770
771After having this in the first part of .xs file, the "Perl glue" part becomes
772as simple as
773
774 int
775 is_even(input)
776 int input
777
778This technique of separation of the glue part from the workhorse part has
779obvious tradeoffs: if you want to change a Perl interface, you need to
780change two places in your code. However, it removes a lot of clutter,
781and makes the workhorse part independent from idiosyncrasies of Perl calling
782convention. (In fact, there is nothing Perl-specific in the above description,
783a different version of B<xsubpp> might have translated this to TCL glue or
784Python glue as well.)
785
786=head2 More about XSUB arguments
4633a7c4 787
791fa977 788With the completion of Example 4, we now have an easy way to simulate some
c07a80fd 789real-life libraries whose interfaces may not be the cleanest in the world.
790We shall now continue with a discussion of the arguments passed to the
beb31b0b 791B<xsubpp> compiler.
4633a7c4 792
360e660c 793When you specify arguments to routines in the .xs file, you are really
794passing three pieces of information for each argument listed. The first
795piece is the order of that argument relative to the others (first, second,
796etc). The second is the type of argument, and consists of the type
797declaration of the argument (e.g., int, char*, etc). The third piece is
beb31b0b 798the calling convention for the argument in the call to the library function.
799
800While Perl passes arguments to functions by reference,
801C passes arguments by value; to implement a C function which modifies data
802of one of the "arguments", the actual argument of this C function would be
803a pointer to the data. Thus two C functions with declarations
804
805 int string_length(char *s);
806 int upper_case_char(char *cp);
807
808may have completely different semantics: the first one may inspect an array
809of chars pointed by s, and the second one may immediately dereference C<cp>
810and manipulate C<*cp> only (using the return value as, say, a success
811indicator). From Perl one would use these functions in
812a completely different manner.
813
814One conveys this info to B<xsubpp> by replacing C<*> before the
815argument by C<&>. C<&> means that the argument should be passed to a library
816function by its address. The above two function may be XSUB-ified as
817
818 int
819 string_length(s)
820 char * s
821
822 int
823 upper_case_char(cp)
824 char &cp
4633a7c4 825
beb31b0b 826For example, consider:
4633a7c4 827
4633a7c4 828 int
c07a80fd 829 foo(a,b)
830 char &a
831 char * b
4633a7c4 832
beb31b0b 833The first Perl argument to this function would be treated as a char and assigned
c07a80fd 834to the variable a, and its address would be passed into the function foo.
beb31b0b 835The second Perl argument would be treated as a string pointer and assigned to the
c07a80fd 836variable b. The I<value> of b would be passed into the function foo. The
beb31b0b 837actual call to the function foo that B<xsubpp> generates would look like this:
4633a7c4 838
c07a80fd 839 foo(&a, b);
4633a7c4 840
beb31b0b 841B<xsubpp> will parse the following function argument lists identically:
791fa977 842
843 char &a
844 char&a
845 char & a
846
847However, to help ease understanding, it is suggested that you place a "&"
848next to the variable name and away from the variable type), and place a
849"*" near the variable type, but away from the variable name (as in the
360e660c 850call to foo above). By doing so, it is easy to understand exactly what
851will be passed to the C function -- it will be whatever is in the "last
852column".
4633a7c4 853
c07a80fd 854You should take great pains to try to pass the function the type of variable
855it wants, when possible. It will save you a lot of trouble in the long run.
4633a7c4 856
360e660c 857=head2 The Argument Stack
4633a7c4 858
c07a80fd 859If we look at any of the C code generated by any of the examples except
860example 1, you will notice a number of references to ST(n), where n is
360e660c 861usually 0. "ST" is actually a macro that points to the n'th argument
862on the argument stack. ST(0) is thus the first argument on the stack and
863therefore the first argument passed to the XSUB, ST(1) is the second
864argument, and so on.
4633a7c4 865
beb31b0b 866When you list the arguments to the XSUB in the .xs file, that tells B<xsubpp>
c07a80fd 867which argument corresponds to which of the argument stack (i.e., the first
868one listed is the first argument, and so on). You invite disaster if you
869do not list them in the same order as the function expects them.
4633a7c4 870
360e660c 871The actual values on the argument stack are pointers to the values passed
872in. When an argument is listed as being an OUTPUT value, its corresponding
873value on the stack (i.e., ST(0) if it was the first argument) is changed.
874You can verify this by looking at the C code generated for Example 3.
875The code for the round() XSUB routine contains lines that look like this:
876
877 double arg = (double)SvNV(ST(0));
878 /* Round the contents of the variable arg */
879 sv_setnv(ST(0), (double)arg);
880
881The arg variable is initially set by taking the value from ST(0), then is
882stored back into ST(0) at the end of the routine.
883
beb31b0b 884XSUBs are also allowed to return lists, not just scalars. This must be
885done by manipulating stack values ST(0), ST(1), etc, in a subtly
886different way. See L<perlxs> for details.
887
888XSUBs are also allowed to avoid automatic conversion of Perl function arguments
889to C function arguments. See L<perlxs> for details. Some people prefer
890manual conversion by inspecting C<ST(i)> even in the cases when automatic
891conversion will do, arguing that this makes the logic of an XSUB call clearer.
892Compare with L<"Getting the fat out of XSUBs"> for a similar tradeoff of
893a complete separation of "Perl glue" and "workhorse" parts of an XSUB.
894
895While experts may argue about these idioms, a novice to Perl guts may
896prefer a way which is as little Perl-guts-specific as possible, meaning
897automatic conversion and automatic call generation, as in
898L<"Getting the fat out of XSUBs">. This approach has the additional
899benefit of protecting the XSUB writer from future changes to the Perl API.
900
360e660c 901=head2 Extending your Extension
4633a7c4 902
c07a80fd 903Sometimes you might want to provide some extra methods or subroutines
904to assist in making the interface between Perl and your extension simpler
905or easier to understand. These routines should live in the .pm file.
906Whether they are automatically loaded when the extension itself is loaded
360e660c 907or only loaded when called depends on where in the .pm file the subroutine
908definition is placed. You can also consult L<Autoloader> for an alternate
909way to store and load your extra subroutines.
4633a7c4 910
360e660c 911=head2 Documenting your Extension
4633a7c4 912
c07a80fd 913There is absolutely no excuse for not documenting your extension.
914Documentation belongs in the .pm file. This file will be fed to pod2man,
360e660c 915and the embedded documentation will be converted to the man page format,
c07a80fd 916then placed in the blib directory. It will be copied to Perl's man
917page directory when the extension is installed.
4633a7c4 918
c07a80fd 919You may intersperse documentation and Perl code within the .pm file.
920In fact, if you want to use method autoloading, you must do this,
921as the comment inside the .pm file explains.
4633a7c4 922
c07a80fd 923See L<perlpod> for more information about the pod format.
4633a7c4 924
360e660c 925=head2 Installing your Extension
4633a7c4 926
c07a80fd 927Once your extension is complete and passes all its tests, installing it
360e660c 928is quite simple: you simply run "make install". You will either need
c07a80fd 929to have write permission into the directories where Perl is installed,
930or ask your system administrator to run the make for you.
4633a7c4 931
360e660c 932Alternately, you can specify the exact directory to place the extension's
933files by placing a "PREFIX=/destination/directory" after the make install.
934(or in between the make and install if you have a brain-dead version of make).
935This can be very useful if you are building an extension that will eventually
936be distributed to multiple systems. You can then just archive the files in
937the destination directory and distribute them to your destination systems.
938
939=head2 EXAMPLE 5
940
941In this example, we'll do some more work with the argument stack. The
942previous examples have all returned only a single value. We'll now
943create an extension that returns an array.
944
945This extension is very Unix-oriented (struct statfs and the statfs system
946call). If you are not running on a Unix system, you can substitute for
947statfs any other function that returns multiple values, you can hard-code
948values to be returned to the caller (although this will be a bit harder
949to test the error case), or you can simply not do this example. If you
950change the XSUB, be sure to fix the test cases to match the changes.
951
952Return to the Mytest directory and add the following code to the end of
953Mytest.xs:
954
955 void
956 statfs(path)
957 char * path
beb31b0b 958 INIT:
360e660c 959 int i;
960 struct statfs buf;
961
962 PPCODE:
963 i = statfs(path, &buf);
964 if (i == 0) {
965 XPUSHs(sv_2mortal(newSVnv(buf.f_bavail)));
966 XPUSHs(sv_2mortal(newSVnv(buf.f_bfree)));
967 XPUSHs(sv_2mortal(newSVnv(buf.f_blocks)));
968 XPUSHs(sv_2mortal(newSVnv(buf.f_bsize)));
969 XPUSHs(sv_2mortal(newSVnv(buf.f_ffree)));
970 XPUSHs(sv_2mortal(newSVnv(buf.f_files)));
971 XPUSHs(sv_2mortal(newSVnv(buf.f_type)));
972 XPUSHs(sv_2mortal(newSVnv(buf.f_fsid[0])));
973 XPUSHs(sv_2mortal(newSVnv(buf.f_fsid[1])));
974 } else {
975 XPUSHs(sv_2mortal(newSVnv(errno)));
976 }
977
978You'll also need to add the following code to the top of the .xs file, just
979after the include of "XSUB.h":
980
981 #include <sys/vfs.h>
982
983Also add the following code segment to test.pl while incrementing the "1..9"
984string in the BEGIN block to "1..11":
985
986 @a = &Mytest::statfs("/blech");
987 print ((scalar(@a) == 1 && $a[0] == 2) ? "ok 10\n" : "not ok 10\n");
988 @a = &Mytest::statfs("/");
989 print scalar(@a) == 9 ? "ok 11\n" : "not ok 11\n";
990
991=head2 New Things in this Example
992
993This example added quite a few new concepts. We'll take them one at a time.
994
995=over 4
996
997=item *
998
beb31b0b 999The INIT: directive contains code that will be placed immediately after
1000the argument stack is decoded. C does not allow variable declarations at
1001arbitrary locations inside a function,
360e660c 1002so this is usually the best way to declare local variables needed by the XSUB.
beb31b0b 1003(Alternatively, one could put the whole C<PPCODE:> section into braces, and
1004put these declarations on top.)
360e660c 1005
1006=item *
1007
1008This routine also returns a different number of arguments depending on the
1009success or failure of the call to statfs. If there is an error, the error
1010number is returned as a single-element array. If the call is successful,
1011then a 9-element array is returned. Since only one argument is passed into
1012this function, we need room on the stack to hold the 9 values which may be
1013returned.
1014
1015We do this by using the PPCODE: directive, rather than the CODE: directive.
beb31b0b 1016This tells B<xsubpp> that we will be managing the return values that will be
360e660c 1017put on the argument stack by ourselves.
1018
1019=item *
1020
1021When we want to place values to be returned to the caller onto the stack,
1022we use the series of macros that begin with "XPUSH". There are five
1023different versions, for placing integers, unsigned integers, doubles,
1024strings, and Perl scalars on the stack. In our example, we placed a
beb31b0b 1025Perl scalar onto the stack. (In fact this is the only macro which
1026can be used to return multiple values.)
360e660c 1027
1028The XPUSH* macros will automatically extend the return stack to prevent
1029it from being overrun. You push values onto the stack in the order you
1030want them seen by the calling program.
1031
1032=item *
1033
1034The values pushed onto the return stack of the XSUB are actually mortal SV's.
1035They are made mortal so that once the values are copied by the calling
1036program, the SV's that held the returned values can be deallocated.
1037If they were not mortal, then they would continue to exist after the XSUB
1038routine returned, but would not be accessible. This is a memory leak.
1039
beb31b0b 1040=item *
1041
1042If we were interested in performance, not in code compactness, in the success
1043branch we would not use C<XPUSHs> macros, but C<PUSHs> macros, and would
1044pre-extend the stack before pushing the return values:
1045
1046 EXTEND(SP, 9);
1047
1048The tradeoff is that one needs to calculate the number of return values
1049in advance (though overextending the stack will not typically hurt
1050anything but memory consumption).
1051
1052Similarly, in the failure branch we could use C<PUSHs> I<without> extending
1053the stack: the Perl function reference comes to an XSUB on the stack, thus
1054the stack is I<always> large enough to take one return value.
1055
360e660c 1056=back
1057
1058=head2 EXAMPLE 6 (Coming Soon)
1059
1060Passing in and returning references to arrays and/or hashes
1061
1062=head2 EXAMPLE 7 (Coming Soon)
1063
1064XPUSH args AND set RETVAL AND assign return value to array
1065
1066=head2 EXAMPLE 8 (Coming Soon)
1067
1068Setting $!
1069
1070=head2 EXAMPLE 9 (Coming Soon)
1071
1072Getting fd's from filehandles
1073
1074=head2 Troubleshooting these Examples
1075
1076As mentioned at the top of this document, if you are having problems with
1077these example extensions, you might see if any of these help you.
1078
1079=over 4
1080
1081=item *
1082
1083In versions of 5.002 prior to the gamma version, the test script in Example
10841 will not function properly. You need to change the "use lib" line to
1085read:
1086
1087 use lib './blib';
1088
1089=item *
1090
1091In versions of 5.002 prior to version 5.002b1h, the test.pl file was not
1092automatically created by h2xs. This means that you cannot say "make test"
1093to run the test script. You will need to add the following line before the
1094"use extension" statement:
1095
1096 use lib './blib';
1097
1098=item *
1099
1100In versions 5.000 and 5.001, instead of using the above line, you will need
1101to use the following line:
1102
1103 BEGIN { unshift(@INC, "./blib") }
1104
1105=item *
1106
1107This document assumes that the executable named "perl" is Perl version 5.
1108Some systems may have installed Perl version 5 as "perl5".
1109
1110=back
1111
1112=head1 See also
4633a7c4 1113
c07a80fd 1114For more information, consult L<perlguts>, L<perlxs>, L<perlmod>,
1115and L<perlpod>.
4633a7c4 1116
360e660c 1117=head1 Author
4633a7c4 1118
9607fc9c 1119Jeff Okamoto <F<okamoto@corp.hp.com>>
4633a7c4 1120
c07a80fd 1121Reviewed and assisted by Dean Roehrich, Ilya Zakharevich, Andreas Koenig,
1122and Tim Bunce.
4633a7c4 1123
c07a80fd 1124=head2 Last Changed
4633a7c4 1125
beb31b0b 11261999/11/30