perlxstut.html

来自「perl教程」· HTML 代码 · 共 907 行 · 第 1/5 页

HTML
907
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlXStut - Tutorial for writing XSUBs</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlXStut - Tutorial for writing XSUBs</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#special_notes">SPECIAL NOTES</a></li>
	<ul>

		<li><a href="#make">make</a></li>
		<li><a href="#version_caveat">Version caveat</a></li>
		<li><a href="#dynamic_loading_versus_static_loading">Dynamic Loading versus Static Loading</a></li>
	</ul>

	<li><a href="#tutorial">TUTORIAL</a></li>
	<ul>

		<li><a href="#example_1">EXAMPLE 1</a></li>
		<li><a href="#example_2">EXAMPLE 2</a></li>
		<li><a href="#what_has_gone_on">What has gone on?</a></li>
		<li><a href="#writing_good_test_scripts">Writing good test scripts</a></li>
		<li><a href="#example_3">EXAMPLE 3</a></li>
		<li><a href="#what_s_new_here">What's new here?</a></li>
		<li><a href="#input_and_output_parameters">Input and Output Parameters</a></li>
		<li><a href="#the_xsubpp_program">The XSUBPP Program</a></li>
		<li><a href="#the_typemap_file">The TYPEMAP file</a></li>
		<li><a href="#warning_about_output_arguments">Warning about Output Arguments</a></li>
		<li><a href="#example_4">EXAMPLE 4</a></li>
		<li><a href="#what_has_happened_here">What has happened here?</a></li>
		<li><a href="#anatomy_of__xs_file">Anatomy of .xs file</a></li>
		<li><a href="#getting_the_fat_out_of_xsubs">Getting the fat out of XSUBs</a></li>
		<li><a href="#more_about_xsub_arguments">More about XSUB arguments</a></li>
		<li><a href="#the_argument_stack">The Argument Stack</a></li>
		<li><a href="#extending_your_extension">Extending your Extension</a></li>
		<li><a href="#documenting_your_extension">Documenting your Extension</a></li>
		<li><a href="#installing_your_extension">Installing your Extension</a></li>
		<li><a href="#example_5">EXAMPLE 5</a></li>
		<li><a href="#new_things_in_this_example">New Things in this Example</a></li>
		<li><a href="#example_6">EXAMPLE 6</a></li>
		<li><a href="#new_things_in_this_example">New Things in this Example</a></li>
		<li><a href="#example_7__coming_soon_">EXAMPLE 7 (Coming Soon)</a></li>
		<li><a href="#example_8__coming_soon_">EXAMPLE 8 (Coming Soon)</a></li>
		<li><a href="#example_9_passing_open_files_to_xses">EXAMPLE 9 Passing open files to XSes</a></li>
		<li><a href="#troubleshooting_these_examples">Troubleshooting these Examples</a></li>
	</ul>

	<li><a href="#see_also">See also</a></li>
	<li><a href="#author">Author</a></li>
	<ul>

		<li><a href="#last_changed">Last Changed</a></li>
	</ul>

</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlXStut - Tutorial for writing XSUBs</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This tutorial will educate the reader on the steps involved in creating
a Perl extension.  The reader is assumed to have access to <a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>,
<a href="../../lib/Pod/perlapi.html">the perlapi manpage</a> and <a href="../../lib/Pod/perlxs.html">the perlxs manpage</a>.</p>
<p>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.</p>
<p>This tutorial was written from a Unix point of view.  Where I know them
to be otherwise different for other platforms (e.g. Win32), I will list
them.  If you find something that was missed, please let me know.</p>
<p>
</p>
<hr />
<h1><a name="special_notes">SPECIAL NOTES</a></h1>
<p>
</p>
<h2><a name="make">make</a></h2>
<p>This tutorial assumes that the make program that Perl is configured to
use is called <code>make</code>.  Instead of running &quot;make&quot; in the examples that
follow, you may have to substitute whatever make program Perl has been
configured to use.  Running <strong>perl -V:make</strong> should tell you what it is.</p>
<p>
</p>
<h2><a name="version_caveat">Version caveat</a></h2>
<p>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.</p>
<p>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 &quot;Troubleshooting these Examples&quot; for more information.</p>
<p>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 <em>README</em> file, but nowadays
installation of extensions may be performed automatically, guided by <em>CPAN.pm</em>
module or other tools.</p>
<p>In MakeMaker-based installations, <em>Makefile.PL</em> provides the earliest
opportunity to perform version checks.  One can put something like this
in <em>Makefile.PL</em> for this purpose:</p>
<pre>
    <span class="keyword">eval</span> <span class="operator">{</span> <span class="keyword">require</span> <span class="number">5.007</span> <span class="operator">}</span>
        <span class="keyword">or</span> <span class="keyword">die</span> <span class="operator">&lt;&lt;</span><span class="default">EOD</span><span class="operator">;</span><span class="string">
    ############
    ### This module uses frobnication framework which is not available before
    ### version 5.007 of Perl.  Upgrade your Perl before installing Kara::Mba.
    ############
    </span><span class="default">EOD</span>
</pre>
<p>
</p>
<h2><a name="dynamic_loading_versus_static_loading">Dynamic Loading versus Static Loading</a></h2>
<p>It is commonly thought that if a system does not have the capability to
dynamically load a library, you cannot build XSUBs.  This is incorrect.
You <em>can</em> 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.</p>
<p>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.</p>
<p>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 &quot;<code>make</code>&quot; with no arguments is executed, run the command
&quot;<code>make perl</code>&quot; instead.</p>
<p>If you have generated such a statically-linked executable by choice, then
instead of saying &quot;<code>make test</code>&quot;, you should say &quot;<code>make test_static</code>&quot;.
On systems that cannot build dynamically-loadable libraries at all, simply
saying &quot;<code>make test</code>&quot; is sufficient.</p>
<p>
</p>
<hr />
<h1><a name="tutorial">TUTORIAL</a></h1>
<p>Now let's go on with the show!</p>
<p>
</p>
<h2><a name="example_1">EXAMPLE 1</a></h2>
<p>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.</p>
<p>Run &quot;<code>h2xs -A -n Mytest</code>&quot;.  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.</p>
<p>The MANIFEST file contains the names of all the files just created in the
Mytest directory.</p>
<p>The file Makefile.PL should look something like this:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">ExtUtils::MakeMaker</span><span class="operator">;</span>
        <span class="comment"># See lib/ExtUtils/MakeMaker.pm for details of how to influence</span>
        <span class="comment"># the contents of the Makefile that is written.</span>
        <span class="variable">WriteMakefile</span><span class="operator">(</span>
            <span class="string">NAME</span>         <span class="operator">=&gt;</span> <span class="string">'Mytest'</span><span class="operator">,</span>
            <span class="string">VERSION_FROM</span> <span class="operator">=&gt;</span> <span class="string">'Mytest.pm'</span><span class="operator">,</span> <span class="comment"># finds $VERSION</span>
            <span class="string">LIBS</span>         <span class="operator">=&gt;</span> <span class="operator">[</span><span class="string">''</span><span class="operator">]</span><span class="operator">,</span>   <span class="comment"># e.g., '-lm'</span>
            <span class="string">DEFINE</span>       <span class="operator">=&gt;</span> <span class="string">''</span><span class="operator">,</span>     <span class="comment"># e.g., '-DHAVE_SOMETHING'</span>
            <span class="string">INC</span>          <span class="operator">=&gt;</span> <span class="string">''</span><span class="operator">,</span>     <span class="comment"># e.g., '-I/usr/include/other'</span>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?