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