Revert "show -E in error message when called with -E"
[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
171891c7 8a Perl extension. The reader is assumed to have access to L<perlguts>,
9L<perlapi> and L<perlxs>.
4633a7c4 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
eb3fb7ac 94MANIFEST, Makefile.PL, Mytest.pm, Mytest.xs, Mytest.t, 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
eb3fb7ac 101 use ExtUtils::MakeMaker;
102 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
103 # the contents of the Makefile that is written.
104 WriteMakefile(
105 NAME => 'Mytest',
106 VERSION_FROM => 'Mytest.pm', # finds $VERSION
107 LIBS => [''], # e.g., '-lm'
108 DEFINE => '', # e.g., '-DHAVE_SOMETHING'
109 INC => '', # e.g., '-I/usr/include/other'
110 );
4633a7c4 111
791fa977 112The file Mytest.pm should start with something like this:
c07a80fd 113
eb3fb7ac 114 package Mytest;
4633a7c4 115
eb3fb7ac 116 use 5.008008;
117 use strict;
118 use warnings;
360e660c 119
eb3fb7ac 120 require Exporter;
c07a80fd 121
eb3fb7ac 122 our @ISA = qw(Exporter);
123 our %EXPORT_TAGS = ( 'all' => [ qw(
c07a80fd 124
eb3fb7ac 125 ) ] );
126
127 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
c07a80fd 128
eb3fb7ac 129 our @EXPORT = qw(
c07a80fd 130
eb3fb7ac 131 );
c07a80fd 132
eb3fb7ac 133 our $VERSION = '0.01';
c07a80fd 134
eb3fb7ac 135 require XSLoader;
136 XSLoader::load('Mytest', $VERSION);
137
138 # Preloaded methods go here.
139
140 1;
141 __END__
142 # Below is the stub of documentation for your module. You better edit it!
4633a7c4 143
360e660c 144The rest of the .pm file contains sample code for providing documentation for
145the extension.
146
147Finally, the Mytest.xs file should look something like this:
4633a7c4 148
eb3fb7ac 149 #include "EXTERN.h"
150 #include "perl.h"
151 #include "XSUB.h"
791fa977 152
eb3fb7ac 153 #include "ppport.h"
154
155 MODULE = Mytest PACKAGE = Mytest
4633a7c4 156
157Let's edit the .xs file by adding this to the end of the file:
158
eb3fb7ac 159 void
160 hello()
161 CODE:
162 printf("Hello, world!\n");
4633a7c4 163
360e660c 164It is okay for the lines starting at the "CODE:" line to not be indented.
165However, for readability purposes, it is suggested that you indent CODE:
166one level and the lines following one more level.
167
168Now we'll run "C<perl Makefile.PL>". This will create a real Makefile,
d9d2a7fb 169which make needs. Its output looks something like:
4633a7c4 170
eb3fb7ac 171 % perl Makefile.PL
172 Checking if your kit is complete...
173 Looks good
174 Writing Makefile for Mytest
175 %
4633a7c4 176
360e660c 177Now, running make will produce output that looks something like this (some
178long lines have been shortened for clarity and some extraneous lines have
179been deleted):
4633a7c4 180
eb3fb7ac 181 % make
182 cp lib/Mytest.pm blib/lib/Mytest.pm
183 perl xsubpp -typemap typemap Mytest.xs > Mytest.xsc && mv Mytest.xsc Mytest.c
184 Please specify prototyping behavior for Mytest.xs (see perlxs manual)
185 cc -c Mytest.c
186 Running Mkbootstrap for Mytest ()
187 chmod 644 Mytest.bs
188 rm -f blib/arch/auto/Mytest/Mytest.so
189 cc -shared -L/usr/local/lib Mytest.o -o blib/arch/auto/Mytest/Mytest.so \
190 \
191
192 chmod 755 blib/arch/auto/Mytest/Mytest.so
193 cp Mytest.bs blib/arch/auto/Mytest/Mytest.bs
194 chmod 644 blib/arch/auto/Mytest/Mytest.bs
195 Manifying blib/man3/Mytest.3pm
196 %
360e660c 197
171891c7 198You can safely ignore the line about "prototyping behavior" - it is
199explained in the section "The PROTOTYPES: Keyword" in L<perlxs>.
360e660c 200
201If you are on a Win32 system, and the build process fails with linker
202errors for functions in the C library, check if your Perl is configured
5a3e7812 203to use PerlCRT (running B<perl -V:libc> should show you if this is the
360e660c 204case). If Perl is configured to use PerlCRT, you have to make sure
205PerlCRT.lib is copied to the same location that msvcrt.lib lives in,
206so that the compiler can find it on its own. msvcrt.lib is usually
207found in the Visual C compiler's lib directory (e.g. C:/DevStudio/VC/lib).
c07a80fd 208
360e660c 209Perl has its own special way of easily writing test scripts, but for this
210example only, we'll create our own test script. Create a file called hello
c07a80fd 211that looks like this:
212
eb3fb7ac 213 #! /opt/perl5/bin/perl
c47ff5f1 214
eb3fb7ac 215 use ExtUtils::testlib;
c47ff5f1 216
eb3fb7ac 217 use Mytest;
c47ff5f1 218
eb3fb7ac 219 Mytest::hello();
4633a7c4 220
f4987be3 221Now we make the script executable (C<chmod +x hello>), run the script
360e660c 222and we should see the following output:
4633a7c4 223
eb3fb7ac 224 % ./hello
225 Hello, world!
226 %
4633a7c4 227
c07a80fd 228=head2 EXAMPLE 2
4633a7c4 229
360e660c 230Now let's add to our extension a subroutine that will take a single numeric
231argument as input and return 0 if the number is even or 1 if the number
232is odd.
4633a7c4 233
791fa977 234Add the following to the end of Mytest.xs:
4633a7c4 235
eb3fb7ac 236 int
237 is_even(input)
238 int input
239 CODE:
240 RETVAL = (input % 2 == 0);
241 OUTPUT:
242 RETVAL
4633a7c4 243
6b0ac556 244There does not need to be whitespace at the start of the "C<int input>"
360e660c 245line, but it is useful for improving readability. Placing a semi-colon at
6b0ac556 246the end of that line is also optional. Any amount and kind of whitespace
360e660c 247may be placed between the "C<int>" and "C<input>".
4633a7c4 248
360e660c 249Now re-run make to rebuild our new shared library.
4633a7c4 250
c07a80fd 251Now perform the same steps as before, generating a Makefile from the
252Makefile.PL file, and running make.
4633a7c4 253
360e660c 254In order to test that our extension works, we now need to look at the
eb3fb7ac 255file Mytest.t. This file is set up to imitate the same kind of testing
c07a80fd 256structure that Perl itself has. Within the test script, you perform a
257number of tests to confirm the behavior of the extension, printing "ok"
eb3fb7ac 258when the test is correct, "not ok" when it is not.
259
260 use Test::More tests => 4;
261 BEGIN { use_ok('Mytest') };
262
263 #########################
c07a80fd 264
eb3fb7ac 265 # Insert your test code below, the Test::More module is use()ed here so read
266 # its man page ( perldoc Test::More ) for help writing this test script.
267
268 is(&Mytest::is_even(0), 1);
269 is(&Mytest::is_even(1), 0);
270 is(&Mytest::is_even(2), 1);
c07a80fd 271
360e660c 272We will be calling the test script through the command "C<make test>". You
c07a80fd 273should see output that looks something like this:
274
eb3fb7ac 275 %make test
276 PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
277 t/Mytest....ok
278 All tests successful.
279 Files=1, Tests=4, 0 wallclock secs ( 0.03 cusr + 0.00 csys = 0.03 CPU)
280 %
4633a7c4 281
360e660c 282=head2 What has gone on?
4633a7c4 283
284The program h2xs is the starting point for creating extensions. In later
c07a80fd 285examples we'll see how we can use h2xs to read header files and generate
4633a7c4 286templates to connect to C routines.
287
288h2xs creates a number of files in the extension directory. The file
289Makefile.PL is a perl script which will generate a true Makefile to build
290the extension. We'll take a closer look at it later.
291
360e660c 292The .pm and .xs files contain the meat of the extension. The .xs file holds
293the C routines that make up the extension. The .pm file contains routines
294that tell Perl how to load your extension.
295
296Generating the Makefile and running C<make> created a directory called blib
297(which stands for "build library") in the current working directory. This
298directory will contain the shared library that we will build. Once we have
299tested it, we can install it into its final location.
300
301Invoking the test script via "C<make test>" did something very important.
302It invoked perl with all those C<-I> arguments so that it could find the
303various files that are part of the extension. It is I<very> important that
304while you are still testing extensions that you use "C<make test>". If you
305try to run the test script all by itself, you will get a fatal error.
306Another reason it is important to use "C<make test>" to run your test
307script is that if you are testing an upgrade to an already-existing version,
6985a70b 308using "C<make test>" ensures that you will test your new extension, not the
360e660c 309already-existing version.
4633a7c4 310
c07a80fd 311When Perl sees a C<use extension;>, it searches for a file with the same name
360e660c 312as the C<use>'d extension that has a .pm suffix. If that file cannot be found,
c07a80fd 313Perl dies with a fatal error. The default search path is contained in the
360e660c 314C<@INC> array.
4633a7c4 315
791fa977 316In our case, Mytest.pm tells perl that it will need the Exporter and Dynamic
360e660c 317Loader extensions. It then sets the C<@ISA> and C<@EXPORT> arrays and the
318C<$VERSION> scalar; finally it tells perl to bootstrap the module. Perl
319will call its dynamic loader routine (if there is one) and load the shared
320library.
4633a7c4 321
360e660c 322The two arrays C<@ISA> and C<@EXPORT> are very important. The C<@ISA>
c07a80fd 323array contains a list of other packages in which to search for methods (or
360e660c 324subroutines) that do not exist in the current package. This is usually
325only important for object-oriented extensions (which we will talk about
326much later), and so usually doesn't need to be modified.
4633a7c4 327
360e660c 328The C<@EXPORT> array tells Perl which of the extension's variables and
329subroutines should be placed into the calling package's namespace. Because
330you don't know if the user has already used your variable and subroutine
331names, it's vitally important to carefully select what to export. Do I<not>
332export method or variable names I<by default> without a good reason.
4633a7c4 333
c07a80fd 334As a general rule, if the module is trying to be object-oriented then don't
360e660c 335export anything. If it's just a collection of functions and variables, then
336you can export them via another array, called C<@EXPORT_OK>. This array
337does not automatically place its subroutine and variable names into the
338namespace unless the user specifically requests that this be done.
4633a7c4 339
c07a80fd 340See L<perlmod> for more information.
4633a7c4 341
360e660c 342The C<$VERSION> variable is used to ensure that the .pm file and the shared
791fa977 343library are "in sync" with each other. Any time you make changes to
344the .pm or .xs files, you should increment the value of this variable.
345
360e660c 346=head2 Writing good test scripts
791fa977 347
353c6505 348The importance of writing good test scripts cannot be over-emphasized. You
791fa977 349should closely follow the "ok/not ok" style that Perl itself uses, so that
350it is very easy and unambiguous to determine the outcome of each test case.
351When you find and fix a bug, make sure you add a test case for it.
352
eb3fb7ac 353By running "C<make test>", you ensure that your Mytest.t script runs and uses
354the correct version of your extension. If you have many test cases,
355save your test files in the "t" directory and use the suffix ".t".
356When you run "C<make test>", all of these test files will be executed.
4633a7c4 357
c07a80fd 358=head2 EXAMPLE 3
4633a7c4 359
360Our third extension will take one argument as its input, round off that
c07a80fd 361value, and set the I<argument> to the rounded value.
4633a7c4 362
791fa977 363Add the following to the end of Mytest.xs:
4633a7c4 364
365 void
366 round(arg)
367 double arg
360e660c 368 CODE:
4633a7c4 369 if (arg > 0.0) {
370 arg = floor(arg + 0.5);
371 } else if (arg < 0.0) {
372 arg = ceil(arg - 0.5);
373 } else {
374 arg = 0.0;
375 }
360e660c 376 OUTPUT:
4633a7c4 377 arg
378
c07a80fd 379Edit the Makefile.PL file so that the corresponding line looks like this:
4633a7c4 380
381 'LIBS' => ['-lm'], # e.g., '-lm'
382
eb3fb7ac 383Generate the Makefile and run make. Change the test number in Mytest.t to
384"9" and add the following tests:
4633a7c4 385
eb3fb7ac 386 $i = -1.5; &Mytest::round($i); is( $i, -2.0 );
387 $i = -1.1; &Mytest::round($i); is( $i, -1.0 );
388 $i = 0.0; &Mytest::round($i); is( $i, 0.0 );
389 $i = 0.5; &Mytest::round($i); is( $i, 1.0 );
390 $i = 1.2; &Mytest::round($i); is( $i, 1.0 );
c07a80fd 391
360e660c 392Running "C<make test>" should now print out that all nine tests are okay.
4633a7c4 393
360e660c 394Notice that in these new test cases, the argument passed to round was a
395scalar variable. You might be wondering if you can round a constant or
eb3fb7ac 396literal. To see what happens, temporarily add the following line to Mytest.t:
4633a7c4 397
791fa977 398 &Mytest::round(3);
4633a7c4 399
360e660c 400Run "C<make test>" and notice that Perl dies with a fatal error. Perl won't
401let you change the value of constants!
4633a7c4 402
360e660c 403=head2 What's new here?
4633a7c4 404
360e660c 405=over 4
4633a7c4 406
360e660c 407=item *
4633a7c4 408
360e660c 409We've made some changes to Makefile.PL. In this case, we've specified an
410extra library to be linked into the extension's shared library, the math
411library libm in this case. We'll talk later about how to write XSUBs that
412can call every routine in a library.
4633a7c4 413
360e660c 414=item *
415
416The value of the function is not being passed back as the function's return
417value, but by changing the value of the variable that was passed into the
418function. You might have guessed that when you saw that the return value
419of round is of type "void".
420
421=back
422
423=head2 Input and Output Parameters
4633a7c4 424
360e660c 425You specify the parameters that will be passed into the XSUB on the line(s)
426after you declare the function's return value and name. Each input parameter
6b0ac556 427line starts with optional whitespace, and may have an optional terminating
360e660c 428semicolon.
4633a7c4 429
360e660c 430The list of output parameters occurs at the very end of the function, just
431before after the OUTPUT: directive. The use of RETVAL tells Perl that you
432wish to send this value back as the return value of the XSUB function. In
433Example 3, we wanted the "return value" placed in the original variable
434which we passed in, so we listed it (and not RETVAL) in the OUTPUT: section.
4633a7c4 435
360e660c 436=head2 The XSUBPP Program
437
beb31b0b 438The B<xsubpp> program takes the XS code in the .xs file and translates it into
4633a7c4 439C code, placing it in a file whose suffix is .c. The C code created makes
440heavy use of the C functions within Perl.
441
360e660c 442=head2 The TYPEMAP file
4633a7c4 443
beb31b0b 444The B<xsubpp> program uses rules to convert from Perl's data types (scalar,
360e660c 445array, etc.) to C's data types (int, char, etc.). These rules are stored
4633a7c4 446in the typemap file ($PERLLIB/ExtUtils/typemap). This file is split into
447three parts.
448
360e660c 449The first section maps various C data types to a name, which corresponds
450somewhat with the various Perl types. The second section contains C code
beb31b0b 451which B<xsubpp> uses to handle input parameters. The third section contains
452C code which B<xsubpp> uses to handle output parameters.
4633a7c4 453
360e660c 454Let's take a look at a portion of the .c file created for our extension.
455The file name is Mytest.c:
4633a7c4 456
791fa977 457 XS(XS_Mytest_round)
4633a7c4 458 {
459 dXSARGS;
c07a80fd 460 if (items != 1)
eb3fb7ac 461 Perl_croak(aTHX_ "Usage: Mytest::round(arg)");
462 PERL_UNUSED_VAR(cv); /* -W */
4633a7c4 463 {
c07a80fd 464 double arg = (double)SvNV(ST(0)); /* XXXXX */
4633a7c4 465 if (arg > 0.0) {
466 arg = floor(arg + 0.5);
467 } else if (arg < 0.0) {
468 arg = ceil(arg - 0.5);
c07a80fd 469 } else {
470 arg = 0.0;
4633a7c4 471 }
360e660c 472 sv_setnv(ST(0), (double)arg); /* XXXXX */
eb3fb7ac 473 SvSETMAGIC(ST(0));
4633a7c4 474 }
eb3fb7ac 475 XSRETURN_EMPTY;
4633a7c4 476 }
4633a7c4 477
360e660c 478Notice the two lines commented with "XXXXX". If you check the first section
479of the typemap file, you'll see that doubles are of type T_DOUBLE. In the
4633a7c4 480INPUT section, an argument that is T_DOUBLE is assigned to the variable
481arg by calling the routine SvNV on something, then casting it to double,
482then assigned to the variable arg. Similarly, in the OUTPUT section,
ef50df4b 483once arg has its final value, it is passed to the sv_setnv function to
484be passed back to the calling subroutine. These two functions are explained
485in L<perlguts>; we'll talk more later about what that "ST(0)" means in the
486section on the argument stack.
4633a7c4 487
360e660c 488=head2 Warning about Output Arguments
4633a7c4 489
c07a80fd 490In general, it's not a good idea to write extensions that modify their input
360e660c 491parameters, as in Example 3. Instead, you should probably return multiple
492values in an array and let the caller handle them (we'll do this in a later
a2293a43 493example). However, in order to better accommodate calling pre-existing C
360e660c 494routines, which often do modify their input parameters, this behavior is
495tolerated.
791fa977 496
497=head2 EXAMPLE 4
498
68dc0745 499In this example, we'll now begin to write XSUBs that will interact with
360e660c 500pre-defined C libraries. To begin with, we will build a small library of
791fa977 501our own, then let h2xs write our .pm and .xs files for us.
502
503Create a new directory called Mytest2 at the same level as the directory
504Mytest. In the Mytest2 directory, create another directory called mylib,
505and cd into that directory.
506
507Here we'll create some files that will generate a test library. These will
508include a C source file and a header file. We'll also create a Makefile.PL
509in this directory. Then we'll make sure that running make at the Mytest2
510level will automatically run this Makefile.PL file and the resulting Makefile.
511
9693b09d 512In the mylib directory, create a file mylib.h that looks like this:
791fa977 513
514 #define TESTVAL 4
515
516 extern double foo(int, long, const char*);
517
518Also create a file mylib.c that looks like this:
519
520 #include <stdlib.h>
521 #include "./mylib.h"
c47ff5f1 522
791fa977 523 double
360e660c 524 foo(int a, long b, const char *c)
791fa977 525 {
526 return (a + b + atof(c) + TESTVAL);
527 }
528
529And finally create a file Makefile.PL that looks like this:
530
531 use ExtUtils::MakeMaker;
532 $Verbose = 1;
533 WriteMakefile(
360e660c 534 NAME => 'Mytest2::mylib',
535 SKIP => [qw(all static static_lib dynamic dynamic_lib)],
49733319 536 clean => {'FILES' => 'libmylib$(LIB_EXT)'},
791fa977 537 );
538
539
8227f81c 540 sub MY::top_targets {
791fa977 541 '
542 all :: static
543
360e660c 544 pure_all :: static
545
791fa977 546 static :: libmylib$(LIB_EXT)
547
548 libmylib$(LIB_EXT): $(O_FILES)
549 $(AR) cr libmylib$(LIB_EXT) $(O_FILES)
550 $(RANLIB) libmylib$(LIB_EXT)
551
552 ';
553 }
554
360e660c 555Make sure you use a tab and not spaces on the lines beginning with "$(AR)"
556and "$(RANLIB)". Make will not function properly if you use spaces.
557It has also been reported that the "cr" argument to $(AR) is unnecessary
558on Win32 systems.
559
791fa977 560We will now create the main top-level Mytest2 files. Change to the directory
561above Mytest2 and run the following command:
562
d9d2a7fb 563 % h2xs -O -n Mytest2 ./Mytest2/mylib/mylib.h
791fa977 564
565This will print out a warning about overwriting Mytest2, but that's okay.
566Our files are stored in Mytest2/mylib, and will be untouched.
567
568The normal Makefile.PL that h2xs generates doesn't know about the mylib
569directory. We need to tell it that there is a subdirectory and that we
360e660c 570will be generating a library in it. Let's add the argument MYEXTLIB to
571the WriteMakefile call so that it looks like this:
4633a7c4 572
360e660c 573 WriteMakefile(
574 'NAME' => 'Mytest2',
575 'VERSION_FROM' => 'Mytest2.pm', # finds $VERSION
576 'LIBS' => [''], # e.g., '-lm'
577 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING'
578 'INC' => '', # e.g., '-I/usr/include/other'
579 'MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)',
580 );
791fa977 581
360e660c 582and then at the end add a subroutine (which will override the pre-existing
583subroutine). Remember to use a tab character to indent the line beginning
584with "cd"!
791fa977 585
586 sub MY::postamble {
587 '
588 $(MYEXTLIB): mylib/Makefile
360e660c 589 cd mylib && $(MAKE) $(PASSTHRU)
791fa977 590 ';
591 }
592
791fa977 593Let's also fix the MANIFEST file so that it accurately reflects the contents
594of our extension. The single line that says "mylib" should be replaced by
595the following three lines:
596
597 mylib/Makefile.PL
598 mylib/mylib.c
599 mylib/mylib.h
600
601To keep our namespace nice and unpolluted, edit the .pm file and change
77ca0c92 602the variable C<@EXPORT> to C<@EXPORT_OK>. Finally, in the
d9d2a7fb 603.xs file, edit the #include line to read:
791fa977 604
605 #include "mylib/mylib.h"
606
607And also add the following function definition to the end of the .xs file:
608
609 double
610 foo(a,b,c)
611 int a
612 long b
613 const char * c
360e660c 614 OUTPUT:
791fa977 615 RETVAL
616
617Now we also need to create a typemap file because the default Perl doesn't
360e660c 618currently support the const char * type. Create a file called typemap in
619the Mytest2 directory and place the following in it:
791fa977 620
621 const char * T_PV
622
623Now run perl on the top-level Makefile.PL. Notice that it also created a
360e660c 624Makefile in the mylib directory. Run make and watch that it does cd into
791fa977 625the mylib directory and run make in there as well.
626
eb3fb7ac 627Now edit the Mytest2.t script and change the number of tests to "4",
791fa977 628and add the following lines to the end of the script:
629
eb3fb7ac 630 is( &Mytest2::foo(1, 2, "Hello, world!"), 7 );
631 is( &Mytest2::foo(1, 2, "0.0"), 7 );
632 ok( abs(&Mytest2::foo(0, 0, "-3.4") - 0.6) <= 0.01 );
791fa977 633
360e660c 634(When dealing with floating-point comparisons, it is best to not check for
635equality, but rather that the difference between the expected and actual
636result is below a certain amount (called epsilon) which is 0.01 in this case)
791fa977 637
eb3fb7ac 638Run "C<make test>" and all should be well. There are some warnings on missing tests
639for the Mytest2::mylib extension, but you can ignore them.
791fa977 640
360e660c 641=head2 What has happened here?
791fa977 642
643Unlike previous examples, we've now run h2xs on a real include file. This
644has caused some extra goodies to appear in both the .pm and .xs files.
645
84dc3c4d 646=over 4
647
791fa977 648=item *
649
360e660c 650In the .xs file, there's now a #include directive with the absolute path to
651the mylib.h header file. We changed this to a relative path so that we
652could move the extension directory if we wanted to.
791fa977 653
654=item *
655
656There's now some new C code that's been added to the .xs file. The purpose
657of the C<constant> routine is to make the values that are #define'd in the
360e660c 658header file accessible by the Perl script (by calling either C<TESTVAL> or
659C<&Mytest2::TESTVAL>). There's also some XS code to allow calls to the
791fa977 660C<constant> routine.
661
662=item *
663
360e660c 664The .pm file originally exported the name C<TESTVAL> in the C<@EXPORT> array.
665This could lead to name clashes. A good rule of thumb is that if the #define
666is only going to be used by the C routines themselves, and not by the user,
667they should be removed from the C<@EXPORT> array. Alternately, if you don't
668mind using the "fully qualified name" of a variable, you could move most
669or all of the items from the C<@EXPORT> array into the C<@EXPORT_OK> array.
791fa977 670
d9d2a7fb 671=item *
672
360e660c 673If our include file had contained #include directives, these would not have
674been processed by h2xs. There is no good solution to this right now.
d9d2a7fb 675
360e660c 676=item *
791fa977 677
678We've also told Perl about the library that we built in the mylib
360e660c 679subdirectory. That required only the addition of the C<MYEXTLIB> variable
791fa977 680to the WriteMakefile call and the replacement of the postamble subroutine
681to cd into the subdirectory and run make. The Makefile.PL for the
682library is a bit more complicated, but not excessively so. Again we
683replaced the postamble subroutine to insert our own code. This code
360e660c 684simply specified that the library to be created here was a static archive
685library (as opposed to a dynamically loadable library) and provided the
791fa977 686commands to build it.
4633a7c4 687
360e660c 688=back
689
beb31b0b 690=head2 Anatomy of .xs file
691
692The .xs file of L<"EXAMPLE 4"> contained some new elements. To understand
693the meaning of these elements, pay attention to the line which reads
694
eb3fb7ac 695 MODULE = Mytest2 PACKAGE = Mytest2
beb31b0b 696
697Anything before this line is plain C code which describes which headers
698to include, and defines some convenience functions. No translations are
7817ba4d 699performed on this part, apart from having embedded POD documentation
700skipped over (see L<perlpod>) it goes into the generated output C file as is.
beb31b0b 701
702Anything after this line is the description of XSUB functions.
703These descriptions are translated by B<xsubpp> into C code which
704implements these functions using Perl calling conventions, and which
705makes these functions visible from Perl interpreter.
706
707Pay a special attention to the function C<constant>. This name appears
708twice in the generated .xs file: once in the first part, as a static C
f4987be3 709function, then another time in the second part, when an XSUB interface to
beb31b0b 710this static C function is defined.
711
712This is quite typical for .xs files: usually the .xs file provides
713an interface to an existing C function. Then this C function is defined
714somewhere (either in an external library, or in the first part of .xs file),
715and a Perl interface to this function (i.e. "Perl glue") is described in the
716second part of .xs file. The situation in L<"EXAMPLE 1">, L<"EXAMPLE 2">,
717and L<"EXAMPLE 3">, when all the work is done inside the "Perl glue", is
718somewhat of an exception rather than the rule.
719
720=head2 Getting the fat out of XSUBs
721
722In L<"EXAMPLE 4"> the second part of .xs file contained the following
723description of an XSUB:
724
725 double
726 foo(a,b,c)
727 int a
728 long b
729 const char * c
730 OUTPUT:
731 RETVAL
732
733Note that in contrast with L<"EXAMPLE 1">, L<"EXAMPLE 2"> and L<"EXAMPLE 3">,
734this description does not contain the actual I<code> for what is done
735is done during a call to Perl function foo(). To understand what is going
736on here, one can add a CODE section to this XSUB:
737
738 double
739 foo(a,b,c)
740 int a
741 long b
742 const char * c
743 CODE:
744 RETVAL = foo(a,b,c);
745 OUTPUT:
746 RETVAL
747
748However, these two XSUBs provide almost identical generated C code: B<xsubpp>
749compiler is smart enough to figure out the C<CODE:> section from the first
750two lines of the description of XSUB. What about C<OUTPUT:> section? In
751fact, that is absolutely the same! The C<OUTPUT:> section can be removed
752as well, I<as far as C<CODE:> section or C<PPCODE:> section> is not
753specified: B<xsubpp> can see that it needs to generate a function call
754section, and will autogenerate the OUTPUT section too. Thus one can
755shortcut the XSUB to become:
756
757 double
758 foo(a,b,c)
759 int a
760 long b
761 const char * c
762
763Can we do the same with an XSUB
764
765 int
766 is_even(input)
767 int input
768 CODE:
769 RETVAL = (input % 2 == 0);
770 OUTPUT:
771 RETVAL
772
773of L<"EXAMPLE 2">? To do this, one needs to define a C function C<int
774is_even(int input)>. As we saw in L<Anatomy of .xs file>, a proper place
775for this definition is in the first part of .xs file. In fact a C function
776
777 int
778 is_even(int arg)
779 {
780 return (arg % 2 == 0);
781 }
782
783is probably overkill for this. Something as simple as a C<#define> will
784do too:
785
786 #define is_even(arg) ((arg) % 2 == 0)
787
788After having this in the first part of .xs file, the "Perl glue" part becomes
789as simple as
790
791 int
792 is_even(input)
793 int input
794
795This technique of separation of the glue part from the workhorse part has
796obvious tradeoffs: if you want to change a Perl interface, you need to
797change two places in your code. However, it removes a lot of clutter,
798and makes the workhorse part independent from idiosyncrasies of Perl calling
799convention. (In fact, there is nothing Perl-specific in the above description,
800a different version of B<xsubpp> might have translated this to TCL glue or
801Python glue as well.)
802
803=head2 More about XSUB arguments
4633a7c4 804
791fa977 805With the completion of Example 4, we now have an easy way to simulate some
c07a80fd 806real-life libraries whose interfaces may not be the cleanest in the world.
807We shall now continue with a discussion of the arguments passed to the
beb31b0b 808B<xsubpp> compiler.
4633a7c4 809
360e660c 810When you specify arguments to routines in the .xs file, you are really
811passing three pieces of information for each argument listed. The first
812piece is the order of that argument relative to the others (first, second,
813etc). The second is the type of argument, and consists of the type
814declaration of the argument (e.g., int, char*, etc). The third piece is
8dcb5783 815the calling convention for the argument in the call to the library function.
beb31b0b 816
817While Perl passes arguments to functions by reference,
818C passes arguments by value; to implement a C function which modifies data
819of one of the "arguments", the actual argument of this C function would be
820a pointer to the data. Thus two C functions with declarations
821
822 int string_length(char *s);
823 int upper_case_char(char *cp);
824
825may have completely different semantics: the first one may inspect an array
826of chars pointed by s, and the second one may immediately dereference C<cp>
827and manipulate C<*cp> only (using the return value as, say, a success
828indicator). From Perl one would use these functions in
829a completely different manner.
830
831One conveys this info to B<xsubpp> by replacing C<*> before the
832argument by C<&>. C<&> means that the argument should be passed to a library
833function by its address. The above two function may be XSUB-ified as
834
835 int
836 string_length(s)
837 char * s
838
839 int
840 upper_case_char(cp)
841 char &cp
4633a7c4 842
beb31b0b 843For example, consider:
4633a7c4 844
4633a7c4 845 int
c07a80fd 846 foo(a,b)
847 char &a
848 char * b
4633a7c4 849
beb31b0b 850The first Perl argument to this function would be treated as a char and assigned
c07a80fd 851to the variable a, and its address would be passed into the function foo.
beb31b0b 852The second Perl argument would be treated as a string pointer and assigned to the
c07a80fd 853variable b. The I<value> of b would be passed into the function foo. The
beb31b0b 854actual call to the function foo that B<xsubpp> generates would look like this:
4633a7c4 855
c07a80fd 856 foo(&a, b);
4633a7c4 857
beb31b0b 858B<xsubpp> will parse the following function argument lists identically:
791fa977 859
860 char &a
861 char&a
862 char & a
863
864However, to help ease understanding, it is suggested that you place a "&"
865next to the variable name and away from the variable type), and place a
866"*" near the variable type, but away from the variable name (as in the
360e660c 867call to foo above). By doing so, it is easy to understand exactly what
868will be passed to the C function -- it will be whatever is in the "last
869column".
4633a7c4 870
c07a80fd 871You should take great pains to try to pass the function the type of variable
872it wants, when possible. It will save you a lot of trouble in the long run.
4633a7c4 873
360e660c 874=head2 The Argument Stack
4633a7c4 875
c07a80fd 876If we look at any of the C code generated by any of the examples except
877example 1, you will notice a number of references to ST(n), where n is
360e660c 878usually 0. "ST" is actually a macro that points to the n'th argument
879on the argument stack. ST(0) is thus the first argument on the stack and
880therefore the first argument passed to the XSUB, ST(1) is the second
881argument, and so on.
4633a7c4 882
beb31b0b 883When you list the arguments to the XSUB in the .xs file, that tells B<xsubpp>
c07a80fd 884which argument corresponds to which of the argument stack (i.e., the first
885one listed is the first argument, and so on). You invite disaster if you
886do not list them in the same order as the function expects them.
4633a7c4 887
360e660c 888The actual values on the argument stack are pointers to the values passed
889in. When an argument is listed as being an OUTPUT value, its corresponding
890value on the stack (i.e., ST(0) if it was the first argument) is changed.
891You can verify this by looking at the C code generated for Example 3.
892The code for the round() XSUB routine contains lines that look like this:
893
894 double arg = (double)SvNV(ST(0));
895 /* Round the contents of the variable arg */
896 sv_setnv(ST(0), (double)arg);
897
898The arg variable is initially set by taking the value from ST(0), then is
899stored back into ST(0) at the end of the routine.
900
beb31b0b 901XSUBs are also allowed to return lists, not just scalars. This must be
902done by manipulating stack values ST(0), ST(1), etc, in a subtly
903different way. See L<perlxs> for details.
904
905XSUBs are also allowed to avoid automatic conversion of Perl function arguments
906to C function arguments. See L<perlxs> for details. Some people prefer
907manual conversion by inspecting C<ST(i)> even in the cases when automatic
908conversion will do, arguing that this makes the logic of an XSUB call clearer.
909Compare with L<"Getting the fat out of XSUBs"> for a similar tradeoff of
910a complete separation of "Perl glue" and "workhorse" parts of an XSUB.
911
912While experts may argue about these idioms, a novice to Perl guts may
913prefer a way which is as little Perl-guts-specific as possible, meaning
914automatic conversion and automatic call generation, as in
915L<"Getting the fat out of XSUBs">. This approach has the additional
916benefit of protecting the XSUB writer from future changes to the Perl API.
917
360e660c 918=head2 Extending your Extension
4633a7c4 919
c07a80fd 920Sometimes you might want to provide some extra methods or subroutines
921to assist in making the interface between Perl and your extension simpler
922or easier to understand. These routines should live in the .pm file.
923Whether they are automatically loaded when the extension itself is loaded
360e660c 924or only loaded when called depends on where in the .pm file the subroutine
4a4eefd0 925definition is placed. You can also consult L<AutoLoader> for an alternate
360e660c 926way to store and load your extra subroutines.
4633a7c4 927
360e660c 928=head2 Documenting your Extension
4633a7c4 929
c07a80fd 930There is absolutely no excuse for not documenting your extension.
931Documentation belongs in the .pm file. This file will be fed to pod2man,
3958b146 932and the embedded documentation will be converted to the manpage format,
933then placed in the blib directory. It will be copied to Perl's
934manpage directory when the extension is installed.
4633a7c4 935
c07a80fd 936You may intersperse documentation and Perl code within the .pm file.
937In fact, if you want to use method autoloading, you must do this,
938as the comment inside the .pm file explains.
4633a7c4 939
c07a80fd 940See L<perlpod> for more information about the pod format.
4633a7c4 941
360e660c 942=head2 Installing your Extension
4633a7c4 943
c07a80fd 944Once your extension is complete and passes all its tests, installing it
8dcb5783 945is quite simple: you simply run "make install". You will either need
c07a80fd 946to have write permission into the directories where Perl is installed,
947or ask your system administrator to run the make for you.
4633a7c4 948
360e660c 949Alternately, you can specify the exact directory to place the extension's
950files by placing a "PREFIX=/destination/directory" after the make install.
951(or in between the make and install if you have a brain-dead version of make).
952This can be very useful if you are building an extension that will eventually
953be distributed to multiple systems. You can then just archive the files in
954the destination directory and distribute them to your destination systems.
955
956=head2 EXAMPLE 5
957
958In this example, we'll do some more work with the argument stack. The
959previous examples have all returned only a single value. We'll now
960create an extension that returns an array.
961
962This extension is very Unix-oriented (struct statfs and the statfs system
963call). If you are not running on a Unix system, you can substitute for
964statfs any other function that returns multiple values, you can hard-code
965values to be returned to the caller (although this will be a bit harder
966to test the error case), or you can simply not do this example. If you
967change the XSUB, be sure to fix the test cases to match the changes.
968
969Return to the Mytest directory and add the following code to the end of
970Mytest.xs:
971
972 void
973 statfs(path)
974 char * path
beb31b0b 975 INIT:
360e660c 976 int i;
977 struct statfs buf;
978
979 PPCODE:
980 i = statfs(path, &buf);
981 if (i == 0) {
982 XPUSHs(sv_2mortal(newSVnv(buf.f_bavail)));
983 XPUSHs(sv_2mortal(newSVnv(buf.f_bfree)));
984 XPUSHs(sv_2mortal(newSVnv(buf.f_blocks)));
985 XPUSHs(sv_2mortal(newSVnv(buf.f_bsize)));
986 XPUSHs(sv_2mortal(newSVnv(buf.f_ffree)));
987 XPUSHs(sv_2mortal(newSVnv(buf.f_files)));
988 XPUSHs(sv_2mortal(newSVnv(buf.f_type)));
360e660c 989 } else {
990 XPUSHs(sv_2mortal(newSVnv(errno)));
991 }
992
993You'll also need to add the following code to the top of the .xs file, just
994after the include of "XSUB.h":
995
996 #include <sys/vfs.h>
997
eb3fb7ac 998Also add the following code segment to Mytest.t while incrementing the "9"
999tests to "11":
360e660c 1000
1001 @a = &Mytest::statfs("/blech");
eb3fb7ac 1002 ok( scalar(@a) == 1 && $a[0] == 2 );
360e660c 1003 @a = &Mytest::statfs("/");
eb3fb7ac 1004 is( scalar(@a), 7 );
360e660c 1005
1006=head2 New Things in this Example
1007
1008This example added quite a few new concepts. We'll take them one at a time.
1009
1010=over 4
1011
1012=item *
1013
beb31b0b 1014The INIT: directive contains code that will be placed immediately after
1015the argument stack is decoded. C does not allow variable declarations at
1016arbitrary locations inside a function,
360e660c 1017so this is usually the best way to declare local variables needed by the XSUB.
beb31b0b 1018(Alternatively, one could put the whole C<PPCODE:> section into braces, and
1019put these declarations on top.)
360e660c 1020
1021=item *
1022
1023This routine also returns a different number of arguments depending on the
1024success or failure of the call to statfs. If there is an error, the error
1025number is returned as a single-element array. If the call is successful,
1026then a 9-element array is returned. Since only one argument is passed into
1027this function, we need room on the stack to hold the 9 values which may be
1028returned.
1029
1030We do this by using the PPCODE: directive, rather than the CODE: directive.
beb31b0b 1031This tells B<xsubpp> that we will be managing the return values that will be
360e660c 1032put on the argument stack by ourselves.
1033
1034=item *
1035
1036When we want to place values to be returned to the caller onto the stack,
1037we use the series of macros that begin with "XPUSH". There are five
1038different versions, for placing integers, unsigned integers, doubles,
1039strings, and Perl scalars on the stack. In our example, we placed a
beb31b0b 1040Perl scalar onto the stack. (In fact this is the only macro which
1041can be used to return multiple values.)
360e660c 1042
1043The XPUSH* macros will automatically extend the return stack to prevent
1044it from being overrun. You push values onto the stack in the order you
1045want them seen by the calling program.
1046
1047=item *
1048
1049The values pushed onto the return stack of the XSUB are actually mortal SV's.
1050They are made mortal so that once the values are copied by the calling
1051program, the SV's that held the returned values can be deallocated.
1052If they were not mortal, then they would continue to exist after the XSUB
1053routine returned, but would not be accessible. This is a memory leak.
1054
beb31b0b 1055=item *
1056
1057If we were interested in performance, not in code compactness, in the success
1058branch we would not use C<XPUSHs> macros, but C<PUSHs> macros, and would
1059pre-extend the stack before pushing the return values:
1060
eb3fb7ac 1061 EXTEND(SP, 7);
beb31b0b 1062
1063The tradeoff is that one needs to calculate the number of return values
1064in advance (though overextending the stack will not typically hurt
1065anything but memory consumption).
1066
1067Similarly, in the failure branch we could use C<PUSHs> I<without> extending
1068the stack: the Perl function reference comes to an XSUB on the stack, thus
1069the stack is I<always> large enough to take one return value.
1070
360e660c 1071=back
1072
171891c7 1073=head2 EXAMPLE 6
360e660c 1074
171891c7 1075In this example, we will accept a reference to an array as an input
1076parameter, and return a reference to an array of hashes. This will
1077demonstrate manipulation of complex Perl data types from an XSUB.
1078
1079This extension is somewhat contrived. It is based on the code in
1080the previous example. It calls the statfs function multiple times,
1081accepting a reference to an array of filenames as input, and returning
1082a reference to an array of hashes containing the data for each of the
1083filesystems.
1084
1085Return to the Mytest directory and add the following code to the end of
1086Mytest.xs:
1087
eb3fb7ac 1088 SV *
1089 multi_statfs(paths)
1090 SV * paths
1091 INIT:
1092 AV * results;
1093 I32 numpaths = 0;
1094 int i, n;
1095 struct statfs buf;
1096
1097 if ((!SvROK(paths))
1098 || (SvTYPE(SvRV(paths)) != SVt_PVAV)
1099 || ((numpaths = av_len((AV *)SvRV(paths))) < 0))
1100 {
1101 XSRETURN_UNDEF;
1102 }
1103 results = (AV *)sv_2mortal((SV *)newAV());
1104 CODE:
1105 for (n = 0; n <= numpaths; n++) {
1106 HV * rh;
1107 STRLEN l;
1108 char * fn = SvPV(*av_fetch((AV *)SvRV(paths), n, 0), l);
1109
1110 i = statfs(fn, &buf);
1111 if (i != 0) {
1112 av_push(results, newSVnv(errno));
1113 continue;
1114 }
171891c7 1115
eb3fb7ac 1116 rh = (HV *)sv_2mortal((SV *)newHV());
1117
1118 hv_store(rh, "f_bavail", 8, newSVnv(buf.f_bavail), 0);
1119 hv_store(rh, "f_bfree", 7, newSVnv(buf.f_bfree), 0);
1120 hv_store(rh, "f_blocks", 8, newSVnv(buf.f_blocks), 0);
1121 hv_store(rh, "f_bsize", 7, newSVnv(buf.f_bsize), 0);
1122 hv_store(rh, "f_ffree", 7, newSVnv(buf.f_ffree), 0);
1123 hv_store(rh, "f_files", 7, newSVnv(buf.f_files), 0);
1124 hv_store(rh, "f_type", 6, newSVnv(buf.f_type), 0);
1125
1126 av_push(results, newRV((SV *)rh));
1127 }
1128 RETVAL = newRV((SV *)results);
1129 OUTPUT:
1130 RETVAL
1131
1132And add the following code to Mytest.t, while incrementing the "11"
1133tests to "13":
171891c7 1134
1135 $results = Mytest::multi_statfs([ '/', '/blech' ]);
eb3fb7ac 1136 ok( ref $results->[0]) );
1137 ok( ! ref $results->[1] );
171891c7 1138
1139=head2 New Things in this Example
1140
1141There are a number of new concepts introduced here, described below:
1142
1143=over 4
1144
1145=item *
1146
1147This function does not use a typemap. Instead, we declare it as accepting
1148one SV* (scalar) parameter, and returning an SV* value, and we take care of
1149populating these scalars within the code. Because we are only returning
1150one value, we don't need a C<PPCODE:> directive - instead, we use C<CODE:>
1151and C<OUTPUT:> directives.
1152
1153=item *
1154
1155When dealing with references, it is important to handle them with caution.
1156The C<INIT:> block first checks that
1157C<SvROK> returns true, which indicates that paths is a valid reference. It
1158then verifies that the object referenced by paths is an array, using C<SvRV>
1159to dereference paths, and C<SvTYPE> to discover its type. As an added test,
1160it checks that the array referenced by paths is non-empty, using the C<av_len>
1161function (which returns -1 if the array is empty). The XSRETURN_UNDEF macro
1162is used to abort the XSUB and return the undefined value whenever all three of
1163these conditions are not met.
1164
1165=item *
1166
1167We manipulate several arrays in this XSUB. Note that an array is represented
1168internally by an AV* pointer. The functions and macros for manipulating
1169arrays are similar to the functions in Perl: C<av_len> returns the highest
1170index in an AV*, much like $#array; C<av_fetch> fetches a single scalar value
1171from an array, given its index; C<av_push> pushes a scalar value onto the
1172end of the array, automatically extending the array as necessary.
1173
1174Specifically, we read pathnames one at a time from the input array, and
1175store the results in an output array (results) in the same order. If
1176statfs fails, the element pushed onto the return array is the value of
1177errno after the failure. If statfs succeeds, though, the value pushed
1178onto the return array is a reference to a hash containing some of the
1179information in the statfs structure.
1180
1181As with the return stack, it would be possible (and a small performance win)
1182to pre-extend the return array before pushing data into it, since we know
1183how many elements we will return:
1184
1185 av_extend(results, numpaths);
1186
1187=item *
1188
1189We are performing only one hash operation in this function, which is storing
1190a new scalar under a key using C<hv_store>. A hash is represented by an HV*
1191pointer. Like arrays, the functions for manipulating hashes from an XSUB
1192mirror the functionality available from Perl. See L<perlguts> and L<perlapi>
1193for details.
1194
1195=item *
1196
1197To create a reference, we use the C<newRV> function. Note that you can
1198cast an AV* or an HV* to type SV* in this case (and many others). This
1199allows you to take references to arrays, hashes and scalars with the same
1200function. Conversely, the C<SvRV> function always returns an SV*, which may
da75cd15 1201need to be cast to the appropriate type if it is something other than a
171891c7 1202scalar (check with C<SvTYPE>).
1203
1204=item *
1205
1206At this point, xsubpp is doing very little work - the differences between
1207Mytest.xs and Mytest.c are minimal.
1208
1209=back
360e660c 1210
1211=head2 EXAMPLE 7 (Coming Soon)
1212
1213XPUSH args AND set RETVAL AND assign return value to array
1214
1215=head2 EXAMPLE 8 (Coming Soon)
1216
1217Setting $!
1218
8dcb5783 1219=head2 EXAMPLE 9 Passing open files to XSes
1220
1221You would think passing files to an XS is difficult, with all the
1222typeglobs and stuff. Well, it isn't.
1223
1224Suppose that for some strange reason we need a wrapper around the
1225standard C library function C<fputs()>. This is all we need:
1226
1227 #define PERLIO_NOT_STDIO 0
1228 #include "EXTERN.h"
1229 #include "perl.h"
1230 #include "XSUB.h"
1231
1232 #include <stdio.h>
1233
1234 int
1235 fputs(s, stream)
1236 char * s
1237 FILE * stream
1238
1239The real work is done in the standard typemap.
1240
1241B<But> you loose all the fine stuff done by the perlio layers. This
1242calls the stdio function C<fputs()>, which knows nothing about them.
1243
e8a52a58 1244The standard typemap offers three variants of PerlIO *:
1245C<InputStream> (T_IN), C<InOutStream> (T_INOUT) and C<OutputStream>
1246(T_OUT). A bare C<PerlIO *> is considered a T_INOUT. If it matters
1247in your code (see below for why it might) #define or typedef
1248one of the specific names and use that as the argument or result
1249type in your XS file.
1250
1251The standard typemap does not contain PerlIO * before perl 5.7,
1252but it has the three stream variants. Using a PerlIO * directly
1253is not backwards compatible unless you provide your own typemap.
22569500 1254
1255For streams coming I<from> perl the main difference is that
1256C<OutputStream> will get the output PerlIO * - which may make
e8a52a58 1257a difference on a socket. Like in our example...
22569500 1258
1259For streams being handed I<to> perl a new file handle is created
1260(i.e. a reference to a new glob) and associated with the PerlIO *
1261provided. If the read/write state of the PerlIO * is not correct then you
1262may get errors or warnings from when the file handle is used.
1263So if you opened the PerlIO * as "w" it should really be an
1264C<OutputStream> if open as "r" it should be an C<InputStream>.
1265
8dcb5783 1266Now, suppose you want to use perlio layers in your XS. We'll use the
1267perlio C<PerlIO_puts()> function as an example.
1268
22569500 1269In the C part of the XS file (above the first MODULE line) you
1270have
1271
1272 #define OutputStream PerlIO *
1273 or
1274 typedef PerlIO * OutputStream;
8dcb5783 1275
8dcb5783 1276
1277And this is the XS code:
1278
1279 int
1280 perlioputs(s, stream)
1281 char * s
22569500 1282 OutputStream stream
8dcb5783 1283 CODE:
1284 RETVAL = PerlIO_puts(stream, s);
1285 OUTPUT:
1286 RETVAL
1287
1288We have to use a C<CODE> section because C<PerlIO_puts()> has the arguments
1289reversed compared to C<fputs()>, and we want to keep the arguments the same.
1290
1291Wanting to explore this thoroughly, we want to use the stdio C<fputs()>
e8a52a58 1292on a PerlIO *. This means we have to ask the perlio system for a stdio
1293C<FILE *>:
8dcb5783 1294
1295 int
1296 perliofputs(s, stream)
1297 char * s
e8a52a58 1298 OutputStream stream
8dcb5783 1299 PREINIT:
1300 FILE *fp = PerlIO_findFILE(stream);
1301 CODE:
1302 if (fp != (FILE*) 0) {
1303 RETVAL = fputs(s, fp);
1304 } else {
1305 RETVAL = -1;
1306 }
1307 OUTPUT:
1308 RETVAL
1309
1310Note: C<PerlIO_findFILE()> will search the layers for a stdio
1311layer. If it can't find one, it will call C<PerlIO_exportFILE()> to
1312generate a new stdio C<FILE>. Please only call C<PerlIO_exportFILE()> if
1313you want a I<new> C<FILE>. It will generate one on each call and push a
1314new stdio layer. So don't call it repeatedly on the same
1315file. C<PerlIO()>_findFILE will retrieve the stdio layer once it has been
1316generated by C<PerlIO_exportFILE()>.
1317
1318This applies to the perlio system only. For versions before 5.7,
1319C<PerlIO_exportFILE()> is equivalent to C<PerlIO_findFILE()>.
1320
360e660c 1321=head2 Troubleshooting these Examples
1322
1323As mentioned at the top of this document, if you are having problems with
1324these example extensions, you might see if any of these help you.
1325
1326=over 4
1327
1328=item *
1329
1330In versions of 5.002 prior to the gamma version, the test script in Example
13311 will not function properly. You need to change the "use lib" line to
1332read:
1333
1334 use lib './blib';
1335
1336=item *
1337
1338In versions of 5.002 prior to version 5.002b1h, the test.pl file was not
1339automatically created by h2xs. This means that you cannot say "make test"
1340to run the test script. You will need to add the following line before the
1341"use extension" statement:
1342
1343 use lib './blib';
1344
1345=item *
1346
1347In versions 5.000 and 5.001, instead of using the above line, you will need
1348to use the following line:
1349
1350 BEGIN { unshift(@INC, "./blib") }
1351
1352=item *
1353
8dcb5783 1354This document assumes that the executable named "perl" is Perl version 5.
360e660c 1355Some systems may have installed Perl version 5 as "perl5".
1356
1357=back
1358
1359=head1 See also
4633a7c4 1360
171891c7 1361For more information, consult L<perlguts>, L<perlapi>, L<perlxs>, L<perlmod>,
c07a80fd 1362and L<perlpod>.
4633a7c4 1363
360e660c 1364=head1 Author
4633a7c4 1365
9607fc9c 1366Jeff Okamoto <F<okamoto@corp.hp.com>>
4633a7c4 1367
c07a80fd 1368Reviewed and assisted by Dean Roehrich, Ilya Zakharevich, Andreas Koenig,
1369and Tim Bunce.
4633a7c4 1370
22569500 1371PerlIO material contributed by Lupe Christoph, with some clarification
1372by Nick Ing-Simmons.
8dcb5783 1373
eb3fb7ac 1374Changes for h2xs as of Perl 5.8.x by Renee Baecker
1375
c07a80fd 1376=head2 Last Changed
4633a7c4 1377
eb3fb7ac 13782007/10/11