diff options
author | Gurusamy Sarathy <gsar@cpan.org> | 1999-07-04 01:26:02 +0000 |
---|---|---|
committer | Gurusamy Sarathy <gsar@cpan.org> | 1999-07-04 01:26:02 +0000 |
commit | 360e660cb5db9f75699c74ab35b1d102dc710528 (patch) | |
tree | 79fe8eb623271b23cd666c40078026792e93816d /pod/perlxstut.pod | |
parent | ad78e5492e8cc81959085c550786672cd0db9f47 (diff) | |
download | perl-360e660cb5db9f75699c74ab35b1d102dc710528.tar.gz |
newer version of perlxstut from Jeff Okamoto (slightly edited
for win32 issues)
p4raw-id: //depot/perl@3565
Diffstat (limited to 'pod/perlxstut.pod')
-rw-r--r-- | pod/perlxstut.pod | 706 |
1 files changed, 451 insertions, 255 deletions
diff --git a/pod/perlxstut.pod b/pod/perlxstut.pod index 69a1a25d73..4200140833 100644 --- a/pod/perlxstut.pod +++ b/pod/perlxstut.pod @@ -1,6 +1,6 @@ =head1 NAME -perlXStut - Tutorial for XSUBs +perlXStut - Tutorial for writing XSUBs =head1 DESCRIPTION @@ -10,61 +10,35 @@ L<perlxs>. This tutorial starts with very simple examples and becomes more complex, with each new example adding new features. Certain concepts may not be -completely explained until later in the tutorial to ease the -reader slowly into building extensions. +completely explained until later in the tutorial in order to slowly ease +the reader into building extensions. -=head2 VERSION CAVEAT +This tutorial was written from a Unix point of view. Where I know them +to be otherwise different for other platforms (e.g. Win32), I will list +them. If you find something that was missed, please let me know. -This tutorial tries hard to keep up with the latest development versions -of Perl. This often means that it is sometimes in advance of the latest -released version of Perl, and that certain features described here might -not work on earlier versions. This section will keep track of when various -features were added to Perl 5. - -=over 4 - -=item * - -In versions of Perl 5.002 prior to the gamma version, the test script -in Example 1 will not function properly. You need to change the "use -lib" line to read: - - use lib './blib'; - -=item * - -In versions of Perl 5.002 prior to version beta 3, the line in the .xs file -about "PROTOTYPES: DISABLE" will cause a compiler error. Simply remove that -line from the file. - -=item * - -In versions of Perl 5.002 prior to version 5.002b1h, the test.pl file was not -automatically created by h2xs. This means that you cannot say "make test" -to run the test script. You will need to add the following line before the -"use extension" statement: +=head1 SPECIAL NOTES - use lib './blib'; - -=item * - -In versions 5.000 and 5.001, instead of using the above line, you will need -to use the following line: +=head2 make - BEGIN { unshift(@INC, "./blib") } +This tutorial assumes that the make program that Perl is configured to +use is called C<make>. Instead of running "make" in the examples that +follow, you may have to substitute whatever make program Perl has been +configured to use. Running "perl -V:make" should tell you what it is. -=item * - -This document assumes that the executable named "perl" is Perl version 5. -Some systems may have installed Perl version 5 as "perl5". +=head2 Version caveat -=back +This tutorial tries hard to keep up with the latest development versions +of Perl. This often means that it is sometimes in advance of the latest +released version of Perl, and that certain features described here might +not work on earlier versions. See the section on "Troubleshooting +these Examples" for more information. -=head2 DYNAMIC VERSUS STATIC +=head2 Dynamic Loading versus Static Loading It is commonly thought that if a system does not have the capability to -load a library dynamically, you cannot build XSUBs. This is incorrect. -You I<can> build them, but you must link the XSUB's subroutines with the +dynamically load a library, you cannot build XSUBs. This is incorrect. +You I<can> build them, but you must link the XSUBs subroutines with the rest of Perl, creating a new executable. This situation is similar to Perl 4. @@ -75,25 +49,30 @@ executable with that static library linked in. Should you wish to build a statically-linked executable on a system which can dynamically load libraries, you may, in all the following examples, -where the command "make" with no arguments is executed, run the command -"make perl" instead. +where the command "C<make>" with no arguments is executed, run the command +"C<make perl>" instead. If you have generated such a statically-linked executable by choice, then -instead of saying "make test", you should say "make test_static". On systems -that cannot build dynamically-loadable libraries at all, simply saying "make -test" is sufficient. +instead of saying "C<make test>", you should say "C<make test_static>". +On systems that cannot build dynamically-loadable libraries at all, simply +saying "C<make test>" is sufficient. + +=head1 TUTORIAL + +Now let's go on with the show! =head2 EXAMPLE 1 Our first extension will be very simple. When we call the routine in the extension, it will print out a well-known message and return. -Run C<h2xs -A -n Mytest>. This creates a directory named Mytest, possibly under -ext/ if that directory exists in the current working directory. Several files -will be created in the Mytest dir, including MANIFEST, Makefile.PL, Mytest.pm, -Mytest.xs, test.pl, and Changes. +Run "C<h2xs -A -n Mytest>". This creates a directory named Mytest, +possibly under ext/ if that directory exists in the current working +directory. Several files will be created in the Mytest dir, including +MANIFEST, Makefile.PL, Mytest.pm, Mytest.xs, test.pl, and Changes. -The MANIFEST file contains the names of all the files created. +The MANIFEST file contains the names of all the files just created in the +Mytest directory. The file Makefile.PL should look something like this: @@ -101,17 +80,20 @@ The file Makefile.PL should look something like this: # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( - 'NAME' => 'Mytest', - 'VERSION_FROM' => 'Mytest.pm', # finds $VERSION - 'LIBS' => [''], # e.g., '-lm' - 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' - 'INC' => '', # e.g., '-I/usr/include/other' + NAME => 'Mytest', + VERSION_FROM => 'Mytest.pm', # finds $VERSION + LIBS => [''], # e.g., '-lm' + DEFINE => '', # e.g., '-DHAVE_SOMETHING' + INC => '', # e.g., '-I/usr/include/other' ); The file Mytest.pm should start with something like this: package Mytest; + use strict; + use vars qw($VERSION @ISA @EXPORT); + require Exporter; require DynaLoader; @@ -134,19 +116,14 @@ The file Mytest.pm should start with something like this: __END__ # Below is the stub of documentation for your module. You better edit it! -And the Mytest.xs file should look something like this: +The rest of the .pm file contains sample code for providing documentation for +the extension. + +Finally, the Mytest.xs file should look something like this: - #ifdef __cplusplus - extern "C" { - #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" - #ifdef __cplusplus - } - #endif - - PROTOTYPES: DISABLE MODULE = Mytest PACKAGE = Mytest @@ -154,10 +131,14 @@ Let's edit the .xs file by adding this to the end of the file: void hello() - CODE: + CODE: printf("Hello, world!\n"); -Now we'll run "perl Makefile.PL". This will create a real Makefile, +It is okay for the lines starting at the "CODE:" line to not be indented. +However, for readability purposes, it is suggested that you indent CODE: +one level and the lines following one more level. + +Now we'll run "C<perl Makefile.PL>". This will create a real Makefile, which make needs. Its output looks something like: % perl Makefile.PL @@ -166,12 +147,14 @@ which make needs. Its output looks something like: Writing Makefile for Mytest % -Now, running make will produce output that looks something like this -(some long lines shortened for clarity): +Now, running make will produce output that looks something like this (some +long lines have been shortened for clarity and some extraneous lines have +been deleted): % make umask 0 && cp Mytest.pm ./blib/Mytest.pm perl xsubpp -typemap typemap Mytest.xs >Mytest.tc && mv Mytest.tc Mytest.c + Please specify prototyping behavior for Mytest.xs (see perlxs manual) cc -c Mytest.c Running Mkbootstrap for Mytest () chmod 644 Mytest.bs @@ -179,55 +162,65 @@ Now, running make will produce output that looks something like this chmod 755 ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl cp Mytest.bs ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs chmod 644 ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs + Manifying ./blib/man3/Mytest.3 + % + +You can safely ignore the line about "prototyping behavior". + +If you are on a Win32 system, and the build process fails with linker +errors for functions in the C library, check if your Perl is configured +to use PerlCRT (running "perl -V:libc" should show you if this is the +case). If Perl is configured to use PerlCRT, you have to make sure +PerlCRT.lib is copied to the same location that msvcrt.lib lives in, +so that the compiler can find it on its own. msvcrt.lib is usually +found in the Visual C compiler's lib directory (e.g. C:/DevStudio/VC/lib). -Now, although there is already a test.pl template ready for us, for this -example only, we'll create a special test script. Create a file called hello +Perl has its own special way of easily writing test scripts, but for this +example only, we'll create our own test script. Create a file called hello that looks like this: #! /opt/perl5/bin/perl - + use ExtUtils::testlib; - + use Mytest; - + Mytest::hello(); -Now we run the script and we should see the following output: +Now we make the script executable (C<chmod -x hello>), run the script +and we should see the following output: - % perl hello + % ./hello Hello, world! % =head2 EXAMPLE 2 -Now let's add to our extension a subroutine that will take a single argument -and return 1 if the argument is even, 0 if the argument is odd. +Now let's add to our extension a subroutine that will take a single numeric +argument as input and return 0 if the number is even or 1 if the number +is odd. Add the following to the end of Mytest.xs: int is_even(input) int input - CODE: + CODE: RETVAL = (input % 2 == 0); - OUTPUT: + OUTPUT: RETVAL -There does not need to be white space at the start of the "int input" line, -but it is useful for improving readability. The semi-colon at the end of -that line is also optional. - -Any white space may be between the "int" and "input". It is also okay for -the four lines starting at the "CODE:" line to not be indented. However, -for readability purposes, it is suggested that you indent them 8 spaces -(or one normal tab stop). +There does not need to be white space at the start of the "C<int input>" +line, but it is useful for improving readability. Placing a semi-colon at +the end of that line is also optional. Any amount and kind of white space +may be placed between the "C<int>" and "C<input>". -Now rerun make to rebuild our new shared library. +Now re-run make to rebuild our new shared library. Now perform the same steps as before, generating a Makefile from the Makefile.PL file, and running make. -To test that our extension works, we now need to look at the +In order to test that our extension works, we now need to look at the file test.pl. This file is set up to imitate the same kind of testing structure that Perl itself has. Within the test script, you perform a number of tests to confirm the behavior of the extension, printing "ok" @@ -239,11 +232,11 @@ to the end of the file: print &Mytest::is_even(1) == 0 ? "ok 3" : "not ok 3", "\n"; print &Mytest::is_even(2) == 1 ? "ok 4" : "not ok 4", "\n"; -We will be calling the test script through the command "make test". You +We will be calling the test script through the command "C<make test>". You should see output that looks something like this: % make test - PERL_DL_NONLAZY=1 /opt/perl5.002b2/bin/perl (lots of -I arguments) test.pl + PERL_DL_NONLAZY=1 /opt/perl5.004/bin/perl (lots of -I arguments) test.pl 1..4 ok 1 ok 2 @@ -251,7 +244,7 @@ should see output that looks something like this: ok 4 % -=head2 WHAT HAS GONE ON? +=head2 What has gone on? The program h2xs is the starting point for creating extensions. In later examples we'll see how we can use h2xs to read header files and generate @@ -261,71 +254,72 @@ h2xs creates a number of files in the extension directory. The file Makefile.PL is a perl script which will generate a true Makefile to build the extension. We'll take a closer look at it later. -The files E<lt>extensionE<gt>.pm and E<lt>extensionE<gt>.xs contain the meat -of the extension. -The .xs file holds the C routines that make up the extension. The .pm file -contains routines that tell Perl how to load your extension. - -Generating and invoking the Makefile created a directory blib (which stands -for "build library") in the current working directory. This directory will -contain the shared library that we will build. Once we have tested it, we -can install it into its final location. - -Invoking the test script via "make test" did something very important. It -invoked perl with all those C<-I> arguments so that it could find the various -files that are part of the extension. - -It is I<very> important that while you are still testing extensions that -you use "make test". If you try to run the test script all by itself, you -will get a fatal error. - -Another reason it is important to use "make test" to run your test script -is that if you are testing an upgrade to an already-existing version, using -"make test" insures that you use your new extension, not the already-existing -version. +The .pm and .xs files contain the meat of the extension. The .xs file holds +the C routines that make up the extension. The .pm file contains routines +that tell Perl how to load your extension. + +Generating the Makefile and running C<make> created a directory called blib +(which stands for "build library") in the current working directory. This +directory will contain the shared library that we will build. Once we have +tested it, we can install it into its final location. + +Invoking the test script via "C<make test>" did something very important. +It invoked perl with all those C<-I> arguments so that it could find the +various files that are part of the extension. It is I<very> important that +while you are still testing extensions that you use "C<make test>". If you +try to run the test script all by itself, you will get a fatal error. +Another reason it is important to use "C<make test>" to run your test +script is that if you are testing an upgrade to an already-existing version, +using "C<make test>" insures that you will test your new extension, not the +already-existing version. When Perl sees a C<use extension;>, it searches for a file with the same name -as the use'd extension that has a .pm suffix. If that file cannot be found, +as the C<use>'d extension that has a .pm suffix. If that file cannot be found, Perl dies with a fatal error. The default search path is contained in the -@INC array. +C<@INC> array. In our case, Mytest.pm tells perl that it will need the Exporter and Dynamic -Loader extensions. It then sets the @ISA and @EXPORT arrays and the $VERSION -scalar; finally it tells perl to bootstrap the module. Perl will call its -dynamic loader routine (if there is one) and load the shared library. +Loader extensions. It then sets the C<@ISA> and C<@EXPORT> arrays and the +C<$VERSION> scalar; finally it tells perl to bootstrap the module. Perl +will call its dynamic loader routine (if there is one) and load the shared +library. -The two arrays that are set in the .pm file are very important. The @ISA +The two arrays C<@ISA> and C<@EXPORT> are very important. The C<@ISA> array contains a list of other packages in which to search for methods (or -subroutines) that do not exist in the current package. The @EXPORT array -tells Perl which of the extension's routines should be placed into the -calling package's namespace. +subroutines) that do not exist in the current package. This is usually +only important for object-oriented extensions (which we will talk about +much later), and so usually doesn't need to be modified. -It's important to select what to export carefully. Do NOT export method names -and do NOT export anything else I<by default> without a good reason. +The C<@EXPORT> array tells Perl which of the extension's variables and +subroutines should be placed into the calling package's namespace. Because +you don't know if the user has already used your variable and subroutine +names, it's vitally important to carefully select what to export. Do I<not> +export method or variable names I<by default> without a good reason. As a general rule, if the module is trying to be object-oriented then don't -export anything. If it's just a collection of functions then you can export -any of the functions via another array, called @EXPORT_OK. +export anything. If it's just a collection of functions and variables, then +you can export them via another array, called C<@EXPORT_OK>. This array +does not automatically place its subroutine and variable names into the +namespace unless the user specifically requests that this be done. See L<perlmod> for more information. -The $VERSION variable is used to ensure that the .pm file and the shared +The C<$VERSION> variable is used to ensure that the .pm file and the shared library are "in sync" with each other. Any time you make changes to the .pm or .xs files, you should increment the value of this variable. -=head2 WRITING GOOD TEST SCRIPTS +=head2 Writing good test scripts The importance of writing good test scripts cannot be overemphasized. You should closely follow the "ok/not ok" style that Perl itself uses, so that it is very easy and unambiguous to determine the outcome of each test case. When you find and fix a bug, make sure you add a test case for it. -By running "make test", you ensure that your test.pl script runs and uses +By running "C<make test>", you ensure that your test.pl script runs and uses the correct version of your extension. If you have many test cases, you -might want to copy Perl's test style. Create a directory named "t", and -ensure all your test files end with the suffix ".t". The Makefile will -properly run all these test files. - +might want to copy Perl's test style. Create a directory named "t" in the +extension's directory and append the suffix ".t" to the names of your test +files. When you run "C<make test>", all of these test files will be executed. =head2 EXAMPLE 3 @@ -337,7 +331,7 @@ Add the following to the end of Mytest.xs: void round(arg) double arg - CODE: + CODE: if (arg > 0.0) { arg = floor(arg + 0.5); } else if (arg < 0.0) { @@ -345,14 +339,14 @@ Add the following to the end of Mytest.xs: } else { arg = 0.0; } - OUTPUT: + OUTPUT: arg Edit the Makefile.PL file so that the corresponding line looks like this: 'LIBS' => ['-lm'], # e.g., '-lm' -Generate the Makefile and run make. Change the BEGIN block to print out +Generate the Makefile and run make. Change the BEGIN block to print "1..9" and add the following to test.pl: $i = -1.5; &Mytest::round($i); print $i == -2.0 ? "ok 5" : "not ok 5", "\n"; @@ -361,58 +355,70 @@ Generate the Makefile and run make. Change the BEGIN block to print out $i = 0.5; &Mytest::round($i); print $i == 1.0 ? "ok 8" : "not ok 8", "\n"; $i = 1.2; &Mytest::round($i); print $i == 1.0 ? "ok 9" : "not ok 9", "\n"; -Running "make test" should now print out that all nine tests are okay. +Running "C<make test>" should now print out that all nine tests are okay. -You might be wondering if you can round a constant. To see what happens, add -the following line to test.pl temporarily: +Notice that in these new test cases, the argument passed to round was a +scalar variable. You might be wondering if you can round a constant or +literal. To see what happens, temporarily add the following line to test.pl: &Mytest::round(3); -Run "make test" and notice that Perl dies with a fatal error. Perl won't let -you change the value of constants! +Run "C<make test>" and notice that Perl dies with a fatal error. Perl won't +let you change the value of constants! -=head2 WHAT'S NEW HERE? +=head2 What's new here? -Two things are new here. First, we've made some changes to Makefile.PL. -In this case, we've specified an extra library to link in, the math library -libm. We'll talk later about how to write XSUBs that can call every routine -in a library. +=over 4 -Second, the value of the function is being passed back not as the function's -return value, but through the same variable that was passed into the function. +=item * -=head2 INPUT AND OUTPUT PARAMETERS +We've made some changes to Makefile.PL. In this case, we've specified an +extra library to be linked into the extension's shared library, the math +library libm in this case. We'll talk later about how to write XSUBs that +can call every routine in a library. -You specify the parameters that will be passed into the XSUB just after you -declare the function return value and name. Each parameter line starts with -optional white space, and may have an optional terminating semicolon. +=item * + +The value of the function is not being passed back as the function's return +value, but by changing the value of the variable that was passed into the +function. You might have guessed that when you saw that the return value +of round is of type "void". + +=back + +=head2 Input and Output Parameters -The list of output parameters occurs after the OUTPUT: directive. The use -of RETVAL tells Perl that you wish to send this value back as the return -value of the XSUB function. In Example 3, the value we wanted returned was -contained in the same variable we passed in, so we listed it (and not RETVAL) -in the OUTPUT: section. +You specify the parameters that will be passed into the XSUB on the line(s) +after you declare the function's return value and name. Each input parameter +line starts with optional white space, and may have an optional terminating +semicolon. -=head2 THE XSUBPP COMPILER +The list of output parameters occurs at the very end of the function, just +before after the OUTPUT: directive. The use of RETVAL tells Perl that you +wish to send this value back as the return value of the XSUB function. In +Example 3, we wanted the "return value" placed in the original variable +which we passed in, so we listed it (and not RETVAL) in the OUTPUT: section. -The compiler xsubpp takes the XS code in the .xs file and converts it into +=head2 The XSUBPP Program + +The xsubpp program takes the XS code in the .xs file and translates it into C code, placing it in a file whose suffix is .c. The C code created makes heavy use of the C functions within Perl. -=head2 THE TYPEMAP FILE +=head2 The TYPEMAP file -The xsubpp compiler uses rules to convert from Perl's data types (scalar, -array, etc.) to C's data types (int, char *, etc.). These rules are stored +The xsubpp program uses rules to convert from Perl's data types (scalar, +array, etc.) to C's data types (int, char, etc.). These rules are stored in the typemap file ($PERLLIB/ExtUtils/typemap). This file is split into three parts. -The first part attempts to map various C data types to a coded flag, which -has some correspondence with the various Perl types. The second part contains -C code which xsubpp uses for input parameters. The third part contains C -code which xsubpp uses for output parameters. We'll talk more about the -C code later. +The first section maps various C data types to a name, which corresponds +somewhat with the various Perl types. The second section contains C code +which xsubpp uses to handle input parameters. The third section contains +C code which xsubpp uses to handle output parameters. -Let's now take a look at a portion of the .c file created for our extension. +Let's take a look at a portion of the .c file created for our extension. +The file name is Mytest.c: XS(XS_Mytest_round) { @@ -428,13 +434,13 @@ Let's now take a look at a portion of the .c file created for our extension. } else { arg = 0.0; } - sv_setnv(ST(0), (double)arg); /* XXXXX */ + sv_setnv(ST(0), (double)arg); /* XXXXX */ } XSRETURN(1); } -Notice the two lines marked with "XXXXX". If you check the first section of -the typemap file, you'll see that doubles are of type T_DOUBLE. In the +Notice the two lines commented with "XXXXX". If you check the first section +of the typemap file, you'll see that doubles are of type T_DOUBLE. In the INPUT section, an argument that is T_DOUBLE is assigned to the variable arg by calling the routine SvNV on something, then casting it to double, then assigned to the variable arg. Similarly, in the OUTPUT section, @@ -443,17 +449,19 @@ be passed back to the calling subroutine. These two functions are explained in L<perlguts>; we'll talk more later about what that "ST(0)" means in the section on the argument stack. -=head2 WARNING +=head2 Warning about Output Arguments In general, it's not a good idea to write extensions that modify their input -parameters, as in Example 3. However, to accommodate better calling -pre-existing C routines, which often do modify their input parameters, -this behavior is tolerated. The next example will show how to do this. +parameters, as in Example 3. Instead, you should probably return multiple +values in an array and let the caller handle them (we'll do this in a later +example). However, in order to better accomodate calling pre-existing C +routines, which often do modify their input parameters, this behavior is +tolerated. =head2 EXAMPLE 4 In this example, we'll now begin to write XSUBs that will interact with -predefined C libraries. To begin with, we will build a small library of +pre-defined C libraries. To begin with, we will build a small library of our own, then let h2xs write our .pm and .xs files for us. Create a new directory called Mytest2 at the same level as the directory @@ -475,12 +483,9 @@ Also create a file mylib.c that looks like this: #include <stdlib.h> #include "./mylib.h" - + double - foo(a, b, c) - int a; - long b; - const char * c; + foo(int a, long b, const char *c) { return (a + b + atof(c) + TESTVAL); } @@ -490,9 +495,9 @@ And finally create a file Makefile.PL that looks like this: use ExtUtils::MakeMaker; $Verbose = 1; WriteMakefile( - NAME => 'Mytest2::mylib', - SKIP => [qw(all static static_lib dynamic dynamic_lib)], - clean => {'FILES' => 'libmylib$(LIB_EXT)'}, + NAME => 'Mytest2::mylib', + SKIP => [qw(all static static_lib dynamic dynamic_lib)], + clean => {'FILES' => 'libmylib$(LIBEEXT)'}, ); @@ -500,6 +505,8 @@ And finally create a file Makefile.PL that looks like this: ' all :: static + pure_all :: static + static :: libmylib$(LIB_EXT) libmylib$(LIB_EXT): $(O_FILES) @@ -509,6 +516,11 @@ And finally create a file Makefile.PL that looks like this: '; } +Make sure you use a tab and not spaces on the lines beginning with "$(AR)" +and "$(RANLIB)". Make will not function properly if you use spaces. +It has also been reported that the "cr" argument to $(AR) is unnecessary +on Win32 systems. + We will now create the main top-level Mytest2 files. Change to the directory above Mytest2 and run the following command: @@ -519,24 +531,29 @@ Our files are stored in Mytest2/mylib, and will be untouched. The normal Makefile.PL that h2xs generates doesn't know about the mylib directory. We need to tell it that there is a subdirectory and that we -will be generating a library in it. Let's add the following key-value -pair to the WriteMakefile call: +will be generating a library in it. Let's add the argument MYEXTLIB to +the WriteMakefile call so that it looks like this: - 'MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)', + WriteMakefile( + 'NAME' => 'Mytest2', + 'VERSION_FROM' => 'Mytest2.pm', # finds $VERSION + 'LIBS' => [''], # e.g., '-lm' + 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' + 'INC' => '', # e.g., '-I/usr/include/other' + 'MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)', + ); -and a new replacement subroutine too: +and then at the end add a subroutine (which will override the pre-existing +subroutine). Remember to use a tab character to indent the line beginning +with "cd"! sub MY::postamble { ' $(MYEXTLIB): mylib/Makefile - cd mylib && $(MAKE) $(PASTHRU) + cd mylib && $(MAKE) $(PASSTHRU) '; } -(Note: Most makes will require that there be a tab character that indents -the line C<cd mylib && $(MAKE) $(PASTHRU)>, similarly for the Makefile in the -subdirectory.) - Let's also fix the MANIFEST file so that it accurately reflects the contents of our extension. The single line that says "mylib" should be replaced by the following three lines: @@ -546,7 +563,7 @@ the following three lines: mylib/mylib.h To keep our namespace nice and unpolluted, edit the .pm file and change -the lines setting @EXPORT to @EXPORT_OK (there are two: one in the line +the variable C<@EXPORT> to C<@EXPORT_OK> (there are two: one in the line beginning "use vars" and one setting the array itself). Finally, in the .xs file, edit the #include line to read: @@ -559,17 +576,17 @@ And also add the following function definition to the end of the .xs file: int a long b const char * c - OUTPUT: + OUTPUT: RETVAL Now we also need to create a typemap file because the default Perl doesn't -currently support the const char * type. Create a file called typemap and -place the following in it: +currently support the const char * type. Create a file called typemap in +the Mytest2 directory and place the following in it: const char * T_PV Now run perl on the top-level Makefile.PL. Notice that it also created a -Makefile in the mylib directory. Run make and see that it does cd into +Makefile in the mylib directory. Run make and watch that it does cd into the mylib directory and run make in there as well. Now edit the test.pl script and change the BEGIN block to print "1..4", @@ -579,13 +596,13 @@ and add the following lines to the end of the script: print &Mytest2::foo(1, 2, "0.0") == 7 ? "ok 3\n" : "not ok 3\n"; print abs(&Mytest2::foo(0, 0, "-3.4") - 0.6) <= 0.01 ? "ok 4\n" : "not ok 4\n"; -(When dealing with floating-point comparisons, it is often useful not to check -for equality, but rather the difference being below a certain epsilon factor, -0.01 in this case) +(When dealing with floating-point comparisons, it is best to not check for +equality, but rather that the difference between the expected and actual +result is below a certain amount (called epsilon) which is 0.01 in this case) -Run "make test" and all should be well. +Run "C<make test>" and all should be well. -=head2 WHAT HAS HAPPENED HERE? +=head2 What has happened here? Unlike previous examples, we've now run h2xs on a real include file. This has caused some extra goodies to appear in both the .pm and .xs files. @@ -594,59 +611,62 @@ has caused some extra goodies to appear in both the .pm and .xs files. =item * -In the .xs file, there's now a #include declaration with the full path to -the mylib.h header file. +In the .xs file, there's now a #include directive with the absolute path to +the mylib.h header file. We changed this to a relative path so that we +could move the extension directory if we wanted to. =item * There's now some new C code that's been added to the .xs file. The purpose of the C<constant> routine is to make the values that are #define'd in the -header file available to the Perl script (in this case, by calling -C<&main::TESTVAL>). There's also some XS code to allow calls to the +header file accessible by the Perl script (by calling either C<TESTVAL> or +C<&Mytest2::TESTVAL>). There's also some XS code to allow calls to the C<constant> routine. =item * -The .pm file has exported the name TESTVAL in the @EXPORT array. This -could lead to name clashes. A good rule of thumb is that if the #define -is going to be used by only the C routines themselves, and not by the user, -they should be removed from the @EXPORT array. Alternately, if you don't -mind using the "fully qualified name" of a variable, you could remove most -or all of the items in the @EXPORT array. +The .pm file originally exported the name C<TESTVAL> in the C<@EXPORT> array. +This could lead to name clashes. A good rule of thumb is that if the #define +is only going to be used by the C routines themselves, and not by the user, +they should be removed from the C<@EXPORT> array. Alternately, if you don't +mind using the "fully qualified name" of a variable, you could move most +or all of the items from the C<@EXPORT> array into the C<@EXPORT_OK> array. =item * -If our include file contained #include directives, these would not be -processed at all by h2xs. There is no good solution to this right now. +If our include file had contained #include directives, these would not have +been processed by h2xs. There is no good solution to this right now. -=back +=item * We've also told Perl about the library that we built in the mylib -subdirectory. That required the addition of only the MYEXTLIB variable +subdirectory. That required only the addition of the C<MYEXTLIB> variable to the WriteMakefile call and the replacement of the postamble subroutine to cd into the subdirectory and run make. The Makefile.PL for the library is a bit more complicated, but not excessively so. Again we replaced the postamble subroutine to insert our own code. This code -specified simply that the library to be created here was a static -archive (as opposed to a dynamically loadable library) and provided the +simply specified that the library to be created here was a static archive +library (as opposed to a dynamically loadable library) and provided the commands to build it. -=head2 SPECIFYING ARGUMENTS TO XSUBPP +=back + +=head2 More about XSUBPP With the completion of Example 4, we now have an easy way to simulate some real-life libraries whose interfaces may not be the cleanest in the world. We shall now continue with a discussion of the arguments passed to the xsubpp compiler. -When you specify arguments in the .xs file, you are really passing three -pieces of information for each one listed. The first piece is the order -of that argument relative to the others (first, second, etc). The second -is the type of argument, and consists of the type declaration of the -argument (e.g., int, char*, etc). The third piece is the exact way in -which the argument should be used in the call to the library function -from this XSUB. This would mean whether or not to place a "&" before -the argument or not, meaning the argument expects to be passed the address -of the specified data type. +When you specify arguments to routines in the .xs file, you are really +passing three pieces of information for each argument listed. The first +piece is the order of that argument relative to the others (first, second, +etc). The second is the type of argument, and consists of the type +declaration of the argument (e.g., int, char*, etc). The third piece is +the exact way in which the argument should be used in the call to the +library function from this XSUB. This would mean whether or not to place +a "&" before the argument or not, meaning the argument expects to be +passed the address of the specified data type. There is a difference between the two arguments in this hypothetical function: @@ -663,7 +683,7 @@ actual call to the function foo that xsubpp generates would look like this: foo(&a, b); -Xsubpp will identically parse the following function argument lists: +Xsubpp will parse the following function argument lists identically: char &a char&a @@ -672,40 +692,55 @@ Xsubpp will identically parse the following function argument lists: However, to help ease understanding, it is suggested that you place a "&" next to the variable name and away from the variable type), and place a "*" near the variable type, but away from the variable name (as in the -complete example above). By doing so, it is easy to understand exactly -what will be passed to the C function -- it will be whatever is in the -"last column". +call to foo above). By doing so, it is easy to understand exactly what +will be passed to the C function -- it will be whatever is in the "last +column". You should take great pains to try to pass the function the type of variable it wants, when possible. It will save you a lot of trouble in the long run. -=head2 THE ARGUMENT STACK +=head2 The Argument Stack If we look at any of the C code generated by any of the examples except example 1, you will notice a number of references to ST(n), where n is -usually 0. The "ST" is actually a macro that points to the n'th argument -on the argument stack. ST(0) is thus the first argument passed to the -XSUB, ST(1) is the second argument, and so on. +usually 0. "ST" is actually a macro that points to the n'th argument +on the argument stack. ST(0) is thus the first argument on the stack and +therefore the first argument passed to the XSUB, ST(1) is the second +argument, and so on. When you list the arguments to the XSUB in the .xs file, that tells xsubpp which argument corresponds to which of the argument stack (i.e., the first one listed is the first argument, and so on). You invite disaster if you do not list them in the same order as the function expects them. -=head2 EXTENDING YOUR EXTENSION +The actual values on the argument stack are pointers to the values passed +in. When an argument is listed as being an OUTPUT value, its corresponding +value on the stack (i.e., ST(0) if it was the first argument) is changed. +You can verify this by looking at the C code generated for Example 3. +The code for the round() XSUB routine contains lines that look like this: + + double arg = (double)SvNV(ST(0)); + /* Round the contents of the variable arg */ + sv_setnv(ST(0), (double)arg); + +The arg variable is initially set by taking the value from ST(0), then is +stored back into ST(0) at the end of the routine. + +=head2 Extending your Extension Sometimes you might want to provide some extra methods or subroutines to assist in making the interface between Perl and your extension simpler or easier to understand. These routines should live in the .pm file. Whether they are automatically loaded when the extension itself is loaded -or loaded only when called depends on where in the .pm file the subroutine -definition is placed. +or only loaded when called depends on where in the .pm file the subroutine +definition is placed. You can also consult L<Autoloader> for an alternate +way to store and load your extra subroutines. -=head2 DOCUMENTING YOUR EXTENSION +=head2 Documenting your Extension There is absolutely no excuse for not documenting your extension. Documentation belongs in the .pm file. This file will be fed to pod2man, -and the embedded documentation will be converted to the manpage format, +and the embedded documentation will be converted to the man page format, then placed in the blib directory. It will be copied to Perl's man page directory when the extension is installed. @@ -715,19 +750,180 @@ as the comment inside the .pm file explains. See L<perlpod> for more information about the pod format. -=head2 INSTALLING YOUR EXTENSION +=head2 Installing your Extension Once your extension is complete and passes all its tests, installing it -is quite simple: you simply run "make install". You will either need +is quite simple: you simply run "make install". You will either need to have write permission into the directories where Perl is installed, or ask your system administrator to run the make for you. -=head2 SEE ALSO +Alternately, you can specify the exact directory to place the extension's +files by placing a "PREFIX=/destination/directory" after the make install. +(or in between the make and install if you have a brain-dead version of make). +This can be very useful if you are building an extension that will eventually +be distributed to multiple systems. You can then just archive the files in +the destination directory and distribute them to your destination systems. + +=head2 EXAMPLE 5 + +In this example, we'll do some more work with the argument stack. The +previous examples have all returned only a single value. We'll now +create an extension that returns an array. + +This extension is very Unix-oriented (struct statfs and the statfs system +call). If you are not running on a Unix system, you can substitute for +statfs any other function that returns multiple values, you can hard-code +values to be returned to the caller (although this will be a bit harder +to test the error case), or you can simply not do this example. If you +change the XSUB, be sure to fix the test cases to match the changes. + +Return to the Mytest directory and add the following code to the end of +Mytest.xs: + + void + statfs(path) + char * path + PREINIT: + int i; + struct statfs buf; + + PPCODE: + i = statfs(path, &buf); + if (i == 0) { + XPUSHs(sv_2mortal(newSVnv(buf.f_bavail))); + XPUSHs(sv_2mortal(newSVnv(buf.f_bfree))); + XPUSHs(sv_2mortal(newSVnv(buf.f_blocks))); + XPUSHs(sv_2mortal(newSVnv(buf.f_bsize))); + XPUSHs(sv_2mortal(newSVnv(buf.f_ffree))); + XPUSHs(sv_2mortal(newSVnv(buf.f_files))); + XPUSHs(sv_2mortal(newSVnv(buf.f_type))); + XPUSHs(sv_2mortal(newSVnv(buf.f_fsid[0]))); + XPUSHs(sv_2mortal(newSVnv(buf.f_fsid[1]))); + } else { + XPUSHs(sv_2mortal(newSVnv(errno))); + } + +You'll also need to add the following code to the top of the .xs file, just +after the include of "XSUB.h": + + #include <sys/vfs.h> + +Also add the following code segment to test.pl while incrementing the "1..9" +string in the BEGIN block to "1..11": + + @a = &Mytest::statfs("/blech"); + print ((scalar(@a) == 1 && $a[0] == 2) ? "ok 10\n" : "not ok 10\n"); + @a = &Mytest::statfs("/"); + print scalar(@a) == 9 ? "ok 11\n" : "not ok 11\n"; + +=head2 New Things in this Example + +This example added quite a few new concepts. We'll take them one at a time. + +=over 4 + +=item * + +The PREINIT: directive contains code that will be placed immediately after +variable declaration and before the argument stack is decoded. Some compilers +cannot handle variable declarations at arbitrary locations inside a function, +so this is usually the best way to declare local variables needed by the XSUB. + +=item * + +This routine also returns a different number of arguments depending on the +success or failure of the call to statfs. If there is an error, the error +number is returned as a single-element array. If the call is successful, +then a 9-element array is returned. Since only one argument is passed into +this function, we need room on the stack to hold the 9 values which may be +returned. + +We do this by using the PPCODE: directive, rather than the CODE: directive. +This tells xsubpp that we will be managing the return values that will be +put on the argument stack by ourselves. + +=item * + +When we want to place values to be returned to the caller onto the stack, +we use the series of macros that begin with "XPUSH". There are five +different versions, for placing integers, unsigned integers, doubles, +strings, and Perl scalars on the stack. In our example, we placed a +Perl scalar onto the stack. + +The XPUSH* macros will automatically extend the return stack to prevent +it from being overrun. You push values onto the stack in the order you +want them seen by the calling program. + +=item * + +The values pushed onto the return stack of the XSUB are actually mortal SV's. +They are made mortal so that once the values are copied by the calling +program, the SV's that held the returned values can be deallocated. +If they were not mortal, then they would continue to exist after the XSUB +routine returned, but would not be accessible. This is a memory leak. + +=back + +=head2 EXAMPLE 6 (Coming Soon) + +Passing in and returning references to arrays and/or hashes + +=head2 EXAMPLE 7 (Coming Soon) + +XPUSH args AND set RETVAL AND assign return value to array + +=head2 EXAMPLE 8 (Coming Soon) + +Setting $! + +=head2 EXAMPLE 9 (Coming Soon) + +Getting fd's from filehandles + +=head2 Troubleshooting these Examples + +As mentioned at the top of this document, if you are having problems with +these example extensions, you might see if any of these help you. + +=over 4 + +=item * + +In versions of 5.002 prior to the gamma version, the test script in Example +1 will not function properly. You need to change the "use lib" line to +read: + + use lib './blib'; + +=item * + +In versions of 5.002 prior to version 5.002b1h, the test.pl file was not +automatically created by h2xs. This means that you cannot say "make test" +to run the test script. You will need to add the following line before the +"use extension" statement: + + use lib './blib'; + +=item * + +In versions 5.000 and 5.001, instead of using the above line, you will need +to use the following line: + + BEGIN { unshift(@INC, "./blib") } + +=item * + +This document assumes that the executable named "perl" is Perl version 5. +Some systems may have installed Perl version 5 as "perl5". + +=back + +=head1 See also For more information, consult L<perlguts>, L<perlxs>, L<perlmod>, and L<perlpod>. -=head2 Author +=head1 Author Jeff Okamoto <F<okamoto@corp.hp.com>> @@ -736,4 +932,4 @@ and Tim Bunce. =head2 Last Changed -1996/7/10 +1999/5/25 |