Mass Deface
should tell you what it is.
=head2 Version caveat
When writing a Perl extension for general consumption, one should expect that
the extension will be used with versions of Perl different from the
version available on your machine. Since you are reading this document,
the version of Perl on your machine is probably 5.005 or later, but the users
of your extension may have more ancient versions.
To understand what kinds of incompatibilities one may expect, and in the rare
case that the version of Perl on your machine is older than this document,
see the section on "Troubleshooting these Examples" for more information.
If your extension uses some features of Perl which are not available on older
releases of Perl, your users would appreciate an early meaningful warning.
You would probably put this information into the F file, but nowadays
installation of extensions may be performed automatically, guided by F
module or other tools.
In MakeMaker-based installations, F provides the earliest
opportunity to perform version checks. One can put something like this
in F for this purpose:
eval { require 5.007 }
or die < build them, but you must link the XSUBs subroutines with the
rest of Perl, creating a new executable. This situation is similar to
Perl 4.
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 "C" with no arguments is executed, run the command
"C" instead.
If you have generated such a statically-linked executable by choice, then
instead of saying "C", you should say "C".
On systems that cannot build dynamically-loadable libraries at all, simply
saying "C" is sufficient.
=head1 TUTORIAL
Now let's go on with the show!
=head2 EXAMPLE 1
Our first extension will be very simple. When we call the routine in the
extension, it will print out a well-known message and return.
Run "C". This creates a directory named Mytest,
possibly under ext/ if that directory exists in the current working
directory. Several files will be created under the Mytest dir, including
MANIFEST, Makefile.PL, lib/Mytest.pm, Mytest.xs, t/Mytest.t, and Changes.
The MANIFEST file contains the names of all the files just created in the
Mytest directory.
The file Makefile.PL should look something like this:
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;
use 5.008008;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
);
our $VERSION = '0.01';
require XSLoader;
XSLoader::load('Mytest', $VERSION);
# Preloaded methods go here.
1;
__END__
# Below is the stub of documentation for your module. You better edit it!
The rest of the .pm file contains sample code for providing documentation for
the extension.
Finally, the Mytest.xs file should look something like this:
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
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");
It is okay for the lines starting at the "CODE:" line to not be indented.
However, for readability purposes, it is suggested that you indent CODE:
one level and the lines following one more level.
Now we'll run "C". This will create a real Makefile,
which make needs. Its 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 have been shortened for clarity and some extraneous lines have
been deleted):
% make
cp lib/Mytest.pm blib/lib/Mytest.pm
perl xsubpp -typemap typemap Mytest.xs > Mytest.xsc && mv Mytest.xsc Mytest.c
Please specify prototyping behavior for Mytest.xs (see perlxs manual)
cc -c Mytest.c
Running Mkbootstrap for Mytest ()
chmod 644 Mytest.bs
rm -f blib/arch/auto/Mytest/Mytest.so
cc -shared -L/usr/local/lib Mytest.o -o blib/arch/auto/Mytest/Mytest.so \
\
chmod 755 blib/arch/auto/Mytest/Mytest.so
cp Mytest.bs blib/arch/auto/Mytest/Mytest.bs
chmod 644 blib/arch/auto/Mytest/Mytest.bs
Manifying blib/man3/Mytest.3pm
%
You can safely ignore the line about "prototyping behavior" - it is
explained in L.
Perl has its own special way of easily writing test scripts, but for this
example only, we'll create our own test script. Create a file called hello
that looks like this:
#! /opt/perl5/bin/perl
use ExtUtils::testlib;
use Mytest;
Mytest::hello();
Now we make the script executable (C), run the script
and we should see the following output:
% ./hello
Hello, world!
%
=head2 EXAMPLE 2
Now let's add to our extension a subroutine that will take a single numeric
argument as input and return 1 if the number is even or 0 if the number
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 whitespace at the start of the "C"
line, but it is useful for improving readability. Placing a semi-colon at
the end of that line is also optional. Any amount and kind of whitespace
may be placed between the "C" and "C".
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 Mytest.t. 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.
use Test::More tests => 4;
BEGIN { use_ok('Mytest') };
#########################
# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.
is(&Mytest::is_even(0), 1);
is(&Mytest::is_even(1), 0);
is(&Mytest::is_even(2), 1);
We will be calling the test script through the command "C". You
should see output that looks something like this:
%make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/Mytest....ok
All tests successful.
Files=1, Tests=4, 0 wallclock secs ( 0.03 cusr + 0.00 csys = 0.03 CPU)
%
=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 .pm and .xs files contain the meat of the extension. The .xs file holds
the C routines that make up the extension. The .pm file contains routines
that tell Perl how to load your extension.
Generating the Makefile and running C created a directory called blib
(which stands for "build library") in the current working directory. This
directory will contain the shared library that we will build. Once we have
tested it, we can install it into its final location.
Invoking the test script via "C" did something very important.
It invoked perl with all those C<-I> arguments so that it could find the
various files that are part of the extension. It is I important that
while you are still testing extensions that you use "C". If you
try to run the test script all by itself, you will get a fatal error.
Another reason it is important to use "C" to run your test
script is that if you are testing an upgrade to an already-existing version,
using "C" ensures that you will test your new extension, not the
already-existing version.
When Perl sees a C