home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D13
/
PERL2.TGZ
/
perl2.tar
/
usr
/
lib
/
perl5
/
pod
/
perlxstut.pod
< prev
Wrap
Text File
|
1996-06-28
|
25KB
|
723 lines
=head1 NAME
perlXStut - Tutorial for XSUB's
=head1 DESCRIPTION
This tutorial will educate the reader on the steps involved in creating
a Perl extension. The reader is assumed to have access to L<perlguts> and
L<perlxs>.
This tutorial starts with very simple examples and becomes more complex,
with each new example adding new features. Certain concepts may not be
completely explained until later in the tutorial in order to slowly ease
the reader into building extensions.
=head2 VERSION CAVEAT
This tutorial tries hard to keep up with the latest development versions
of Perl. This often means that it is sometimes in advance of the latest
released version of Perl, and that certain features described here might
not work on earlier versions. This section will keep track of when various
features were added to Perl 5.
=over 4
=item *
In versions of 5.002 prior to version beta 3, then the line in the .xs file
about "PROTOTYPES: DISABLE" will cause a compiler error. Simply remove that
line from the file.
=item *
In versions of 5.002 prior to version 5.002b1h, the test.pl file was not
automatically created by h2xs. This means that you cannot say "make test"
to run the test script. You will need to add the following line before the
"use extension" statement:
use lib './blib';
=item *
In versions 5.000 and 5.001, instead of using the above line, you will need
to use the following line:
BEGIN { unshift(@INC, "./blib") }
=item *
This document assumes that the executable named "perl" is Perl version 5.
Some systems may have installed Perl version 5 as "perl5".
=back
=head2 DYNAMIC VERSUS STATIC
It is commonly thought that if a system does not have the capability to
dynamically load a library, you cannot build XSUB's. This is incorrect.
You I<can> build them, but you must link the XSUB's subroutines with the
rest of Perl, creating a new executable. This situation is similar to
Perl 4.
This tutorial can still be used on such a system. The XSUB build mechanism
will check the system and build a dynamically-loadable library if possible,
or else a static library and then, optionally, a new statically-linked
executable with that static library linked in.
Should you wish to build a statically-linked executable on a system which
can dynamically load libraries, you may, in all the following examples,
where the command "make" with no arguments is executed, run the command
"make perl" instead.
If you have generated such a statically-linked executable by choice, then
instead of saying "make test", you should say "make test_static". On systems
that cannot build dynamically-loadable libraries at all, simply saying "make
test" is sufficient.
=head2 EXAMPLE 1
Our first extension will be very simple. When we call the routine in the
extension, it will print out a well-known message and return.
Run "h2xs -A -n Mytest". This creates a directory named Mytest, possibly under
ext/ if that directory exists in the current working directory. Several files
will be created in the Mytest dir, including MANIFEST, Makefile.PL, Mytest.pm,
Mytest.xs, test.pl, and Changes.
The MANIFEST file contains the names of all the files created.
The file Makefile.PL should look something like this:
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
'NAME' => 'Mytest',
'VERSION_FROM' => 'Mytest.pm', # finds $VERSION
'LIBS' => [''], # e.g., '-lm'
'DEFINE' => '', # e.g., '-DHAVE_SOMETHING'
'INC' => '', # e.g., '-I/usr/include/other'
);
The file Mytest.pm should start with something like this:
package Mytest;
require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
);
$VERSION = '0.01';
bootstrap Mytest $VERSION;
# Preloaded methods go here.
# Autoload methods go after __END__, and are processed by the autosplit program.
1;
__END__
# Below is the stub of documentation for your module. You better edit it!
And the Mytest.xs file should look something like this:
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
PROTOTYPES: DISABLE
MODULE = Mytest PACKAGE = Mytest
Let's edit the .xs file by adding this to the end of the file:
void
hello()
CODE:
printf("Hello, world!\n");
Now we'll run "perl Makefile.PL". This will create a real Makefile,
which make needs. It's output looks something like:
% perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for Mytest
%
Now, running make will produce output that looks something like this
(some long lines shortened for clarity):
% make
umask 0 && cp Mytest.pm ./blib/Mytest.pm
perl xsubpp -typemap typemap Mytest.xs >Mytest.tc && mv Mytest.tc Mytest.c
cc -c Mytest.c
Running Mkbootstrap for Mytest ()
chmod 644 Mytest.bs
LD_RUN_PATH="" ld -o ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl -b Mytest.o
chmod 755 ./blib/PA-RISC1.1/auto/Mytest/Mytest.sl
cp Mytest.bs ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs
chmod 644 ./blib/PA-RISC1.1/auto/Mytest/Mytest.bs
Now, although there is already a test.pl template ready for us, for this
example only, we'll create a special test script. Create a file called hello
that looks like this:
#! /opt/perl5/bin/perl
use lib './blib';
use Mytest;
Mytest::hello();
Now we run the script and we should see the following output:
% perl hello
Hello, world!
%
=head2 EXAMPLE 2
Now let's add to our extension a subroutine that will take a single argument
and return 0 if the argument is even, 1 if the argument is odd.
Add the following to the end of Mytest.xs:
int
is_even(input)
int input
CODE:
RETVAL = (input % 2 == 0);
OUTPUT:
RETVAL
There does not need to be white space at the start of the "int input" line,
but it is useful for improving readability. The semi-colon at the end of
that line is also optional.
Any white space may be between the "int" and "input". It is also okay for
the four lines starting at the "CODE:" line to not be indented. However,
for readability purposes, it is suggested that you indent them 8 spaces
(or one normal tab stop).
Now re-run make to rebuild our new shared library.
Now perform the same steps as before, generating a Makefile from the
Makefile.PL file, and running make.
In order to test that our extension works, we now need to look at the
file test.pl. This file is set up to imitate the same kind of testing
structure that Perl itself has. Within the test script, you perform a
number of tests to confirm the behavior of the extension, printing "ok"
when the test is correct, "not ok" when it is not.
Remove the line that starts with "use lib", change the print statement in
the BEGIN block to print "1..4", and add the following code to the end of
the file:
print &Mytest::is_even(0) == 1 ? "ok 2" : "not ok 2", "\n";
print &Mytest::is_even(1) == 0 ? "ok 3" : "not ok 3", "\n";
print &Mytest::is_even(2) == 1 ? "ok 4" : "not ok 4", "\n";
We will be calling the test script through the command "make test". You
should see output that looks something like this:
% make test
PERL_DL_NONLAZY=1 /opt/perl5.002b2/bin/perl (lots of -I arguments) test.pl
1..4
ok 1
ok 2
ok 3
ok 4
%
=head2 WHAT HAS GONE ON?
The program h2xs is the starting point for creating extensions. In later
examples we'll see how we can use h2xs to read header files and generate
templates to connect to C routines.
h2xs creates a number of files in the extension directory. The file
Makefile.PL is a perl script which will generate a true Makefile to build
the extension. We'll take a closer look at it later.
The files <extension>.pm and <extension>.xs contain the meat of the extension.
The .xs file holds the C routines that make up the extension. The .pm file
contains routines that tell Perl how to load your extension.
G