perlxstut.html

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

HTML
907 行
字号
try to run the test script all by itself, you will get a fatal error.
Another reason it is important to use &quot;<code>make test</code>&quot; to run your test
script is that if you are testing an upgrade to an already-existing version,
using &quot;<code>make test</code>&quot; insures that you will test your new extension, not the
already-existing version.</p>
<p>When Perl sees a <code>use extension;</code>, it searches for a file with the same name
as the <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a>'d extension that has a .pm suffix.  If that file cannot be found,
Perl dies with a fatal error.  The default search path is contained in the
<a href="../../lib/Pod/perlvar.html#item__inc"><code>@INC</code></a> array.</p>
<p>In our case, Mytest.pm tells perl that it will need the Exporter and Dynamic
Loader extensions.  It then sets the <code>@ISA</code> and <code>@EXPORT</code> arrays and the
<code>$VERSION</code> scalar; finally it tells perl to bootstrap the module.  Perl
will call its dynamic loader routine (if there is one) and load the shared
library.</p>
<p>The two arrays <code>@ISA</code> and <code>@EXPORT</code> are very important.  The <code>@ISA</code>
array contains a list of other packages in which to search for methods (or
subroutines) that do not exist in the current package.  This is usually
only important for object-oriented extensions (which we will talk about
much later), and so usually doesn't need to be modified.</p>
<p>The <code>@EXPORT</code> array tells Perl which of the extension's variables and
subroutines should be placed into the calling package's namespace.  Because
you don't know if the user has already used your variable and subroutine
names, it's vitally important to carefully select what to export.  Do <em>not</em>
export method or variable names <em>by default</em> without a good reason.</p>
<p>As a general rule, if the module is trying to be object-oriented then don't
export anything.  If it's just a collection of functions and variables, then
you can export them via another array, called <code>@EXPORT_OK</code>.  This array
does not automatically place its subroutine and variable names into the
namespace unless the user specifically requests that this be done.</p>
<p>See <a href="../../lib/Pod/perlmod.html">the perlmod manpage</a> for more information.</p>
<p>The <code>$VERSION</code> variable is used to ensure that the .pm file and the shared
library are &quot;in sync&quot; with each other.  Any time you make changes to
the .pm or .xs files, you should increment the value of this variable.</p>
<p>
</p>
<h2><a name="writing_good_test_scripts">Writing good test scripts</a></h2>
<p>The importance of writing good test scripts cannot be overemphasized.  You
should closely follow the &quot;ok/not ok&quot; style that Perl itself uses, so that
it is very easy and unambiguous to determine the outcome of each test case.
When you find and fix a bug, make sure you add a test case for it.</p>
<p>By running &quot;<code>make test</code>&quot;, you ensure that your test.pl script runs and uses
the correct version of your extension.  If you have many test cases, you
might want to copy Perl's test style.  Create a directory named &quot;t&quot; in the
extension's directory and append the suffix &quot;.t&quot; to the names of your test
files.  When you run &quot;<code>make test</code>&quot;, all of these test files will be executed.</p>
<p>
</p>
<h2><a name="example_3">EXAMPLE 3</a></h2>
<p>Our third extension will take one argument as its input, round off that
value, and set the <em>argument</em> to the rounded value.</p>
<p>Add the following to the end of Mytest.xs:</p>
<pre>
        <span class="variable">void</span>
        <span class="variable">round</span><span class="operator">(</span><span class="variable">arg</span><span class="operator">)</span>
                <span class="variable">double</span>  <span class="variable">arg</span>
            <span class="variable">CODE</span><span class="operator">:</span>
                <span class="keyword">if</span> <span class="operator">(</span><span class="variable">arg</span> <span class="operator">&gt;</span> <span class="number">0</span><span class="operator">.</span><span class="number">0</span><span class="operator">)</span> <span class="operator">{</span>
                        <span class="variable">arg</span> <span class="operator">=</span> <span class="variable">floor</span><span class="operator">(</span><span class="variable">arg</span> <span class="operator">+</span> <span class="number">0</span><span class="operator">.</span><span class="number">5</span><span class="operator">);</span>
                <span class="operator">}</span> <span class="keyword">else</span> <span class="keyword">if</span> <span class="operator">(</span><span class="variable">arg</span> <span class="operator">&lt;</span> <span class="number">0</span><span class="operator">.</span><span class="number">0</span><span class="operator">)</span> <span class="operator">{</span>
                        <span class="variable">arg</span> <span class="operator">=</span> <span class="variable">ceil</span><span class="operator">(</span><span class="variable">arg</span> <span class="operator">-</span> <span class="number">0</span><span class="operator">.</span><span class="number">5</span><span class="operator">);</span>
                <span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
                        <span class="variable">arg</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">.</span><span class="number">0</span><span class="operator">;</span>
                <span class="operator">}</span>
            <span class="variable">OUTPUT</span><span class="operator">:</span>
                <span class="variable">arg</span>
</pre>
<p>Edit the Makefile.PL file so that the corresponding line looks like this:</p>
<pre>
        'LIBS'      =&gt; ['-lm'],   # e.g., '-lm'</pre>
<p>Generate the Makefile and run make.  Change the BEGIN block to print
&quot;1..9&quot; and add the following to test.pl:</p>
<pre>
        <span class="variable">$i</span> <span class="operator">=</span> <span class="operator">-</span><span class="number">1.5</span><span class="operator">;</span> <span class="operator">&amp;</span><span class="variable">Mytest::round</span><span class="operator">(</span><span class="variable">$i</span><span class="operator">);</span> <span class="keyword">print</span> <span class="variable">$i</span> <span class="operator">==</span> <span class="operator">-</span><span class="number">2.0</span> <span class="operator">?</span> <span class="string">"ok 5"</span> <span class="operator">:</span> <span class="string">"not ok 5"</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
        <span class="variable">$i</span> <span class="operator">=</span> <span class="operator">-</span><span class="number">1.1</span><span class="operator">;</span> <span class="operator">&amp;</span><span class="variable">Mytest::round</span><span class="operator">(</span><span class="variable">$i</span><span class="operator">);</span> <span class="keyword">print</span> <span class="variable">$i</span> <span class="operator">==</span> <span class="operator">-</span><span class="number">1.0</span> <span class="operator">?</span> <span class="string">"ok 6"</span> <span class="operator">:</span> <span class="string">"not ok 6"</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
        <span class="variable">$i</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">.</span><span class="number">0</span><span class="operator">;</span> <span class="operator">&amp;</span><span class="variable">Mytest::round</span><span class="operator">(</span><span class="variable">$i</span><span class="operator">);</span> <span class="keyword">print</span> <span class="variable">$i</span> <span class="operator">==</span> <span class="number">0</span><span class="operator">.</span><span class="number">0</span> <span class="operator">?</span> <span class="string">"ok 7"</span> <span class="operator">:</span> <span class="string">"not ok 7"</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
        <span class="variable">$i</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">.</span><span class="number">5</span><span class="operator">;</span> <span class="operator">&amp;</span><span class="variable">Mytest::round</span><span class="operator">(</span><span class="variable">$i</span><span class="operator">);</span> <span class="keyword">print</span> <span class="variable">$i</span> <span class="operator">==</span> <span class="number">1.0</span> <span class="operator">?</span> <span class="string">"ok 8"</span> <span class="operator">:</span> <span class="string">"not ok 8"</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
        <span class="variable">$i</span> <span class="operator">=</span> <span class="number">1.2</span><span class="operator">;</span> <span class="operator">&amp;</span><span class="variable">Mytest::round</span><span class="operator">(</span><span class="variable">$i</span><span class="operator">);</span> <span class="keyword">print</span> <span class="variable">$i</span> <span class="operator">==</span> <span class="number">1.0</span> <span class="operator">?</span> <span class="string">"ok 9"</span> <span class="operator">:</span> <span class="string">"not ok 9"</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>Running &quot;<code>make test</code>&quot; should now print out that all nine tests are okay.</p>
<p>Notice that in these new test cases, the argument passed to round was a
scalar variable.  You might be wondering if you can round a constant or
literal.  To see what happens, temporarily add the following line to test.pl:</p>
<pre>
        <span class="operator">&amp;</span><span class="variable">Mytest::round</span><span class="operator">(</span><span class="number">3</span><span class="operator">);</span>
</pre>
<p>Run &quot;<code>make test</code>&quot; and notice that Perl dies with a fatal error.  Perl won't
let you change the value of constants!</p>
<p>
</p>
<h2><a name="what_s_new_here">What's new here?</a></h2>
<ul>
<li>
<p>We've made some changes to Makefile.PL.  In this case, we've specified an
extra library to be linked into the extension's shared library, the math
library libm in this case.  We'll talk later about how to write XSUBs that
can call every routine in a library.</p>
</li>
<li>
<p>The value of the function is not being passed back as the function's return
value, but by changing the value of the variable that was passed into the
function.  You might have guessed that when you saw that the return value
of round is of type &quot;void&quot;.</p>
</li>
</ul>
<p>
</p>
<h2><a name="input_and_output_parameters">Input and Output Parameters</a></h2>
<p>You specify the parameters that will be passed into the XSUB on the <code>line(s)</code>
after you declare the function's return value and name.  Each input parameter
line starts with optional whitespace, and may have an optional terminating
semicolon.</p>
<p>The list of output parameters occurs at the very end of the function, just
before after the OUTPUT: directive.  The use of RETVAL tells Perl that you
wish to send this value back as the return value of the XSUB function.  In
Example 3, we wanted the &quot;return value&quot; placed in the original variable
which we passed in, so we listed it (and not RETVAL) in the OUTPUT: section.</p>
<p>
</p>
<h2><a name="the_xsubpp_program">The XSUBPP Program</a></h2>
<p>The <strong>xsubpp</strong> program takes the XS code in the .xs file and translates it into
C code, placing it in a file whose suffix is .c.  The C code created makes
heavy use of the C functions within Perl.</p>
<p>
</p>
<h2><a name="the_typemap_file">The TYPEMAP file</a></h2>
<p>The <strong>xsubpp</strong> program uses rules to convert from Perl's data types (scalar,
array, etc.) to C's data types (int, char, etc.).  These rules are stored
in the typemap file ($PERLLIB/ExtUtils/typemap).  This file is split into
three parts.</p>
<p>The first section maps various C data types to a name, which corresponds
somewhat with the various Perl types.  The second section contains C code
which <strong>xsubpp</strong> uses to handle input parameters.  The third section contains
C code which <strong>xsubpp</strong> uses to handle output parameters.</p>
<p>Let's take a look at a portion of the .c file created for our extension.
The file name is Mytest.c:</p>
<pre>
        <span class="variable">XS</span><span class="operator">(</span><span class="variable">XS_Mytest_round</span><span class="operator">)</span>
        <span class="operator">{</span>
            <span class="variable">dXSARGS</span><span class="operator">;</span>
            <span class="keyword">if</span> <span class="operator">(</span><span class="variable">items</span> <span class="operator">!=</span> <span class="number">1</span><span class="operator">)</span>
                <span class="variable">croak</span><span class="operator">(</span><span class="string">"Usage: Mytest::round(arg)"</span><span class="operator">);</span>
            <span class="operator">{</span>
                <span class="variable">double</span>  <span class="variable">arg</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">double</span><span class="operator">)</span><span class="variable">SvNV</span><span class="operator">(</span><span class="variable">ST</span><span class="operator">(</span><span class="number">0</span><span class="operator">));</span>      <span class="regex">/* XXXXX */</span>
                <span class="keyword">if</span> <span class="operator">(</span><span class="variable">arg</span> <span class="operator">&gt;</span> <span class="number">0</span><span class="operator">.</span><span class="number">0</span><span class="operator">)</span> <span class="operator">{</span>
                        <span class="variable">arg</span> <span class="operator">=</span> <span class="variable">floor</span><span class="operator">(</span><span class="variable">arg</span> <span class="operator">+</span> <span class="number">0</span><span class="operator">.</span><span class="number">5</span><span class="operator">);</span>
                <span class="operator">}</span> <span class="keyword">else</span> <span class="keyword">if</span> <span class="operator">(</span><span class="variable">arg</span> <span class="operator">&lt;</span> <span class="number">0</span><span class="operator">.</span><span class="number">0</span><span class="operator">)</span> <span class="operator">{</span>
                        <span class="variable">arg</span> <span class="operator">=</span> <span class="variable">ceil</span><span class="operator">(</span><span class="variable">arg</span> <span class="operator">-</span> <span class="number">0</span><span class="operator">.</span><span class="number">5</span><span class="operator">);</span>
                <span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
                        <span class="variable">arg</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">.</span><span class="number">0</span><span class="operator">;</span>
                <span class="operator">}</span>
                <span class="variable">sv_setnv</span><span class="operator">(</span><span class="variable">ST</span><span class="operator">(</span><span class="number">0</span><span class="operator">),</span> <span class="operator">(</span><span class="variable">double</span><span class="operator">)</span><span class="variable">arg</span><span class="operator">);</span>   <span class="regex">/* XXXXX */</span>
            <span class="operator">}</span>
            <span class="variable">XSRETURN</span><span class="operator">(</span><span class="number">1</span><span class="operator">);</span>
        <span class="operator">}</span>
</pre>
<p>Notice the two lines commented with &quot;XXXXX&quot;.  If you check the first section
of the typemap file, you'll see that doubles are of type T_DOUBLE.  In the
INPUT section, an argument that is T_DOUBLE is assigned to the variable
arg by calling the routine SvNV on something, then casting it to double,
then assigned to the variable arg.  Similarly, in the OUTPUT section,
once arg has its final value, it is passed to the sv_setnv function to
be passed back to the calling subroutine.  These two functions are explained
in <a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>; we'll talk more later about what that &quot;ST(0)&quot; means in the
section on the argument stack.</p>
<p>
</p>
<h2><a name="warning_about_output_arguments">Warning about Output Arguments</a></h2>
<p>In general, it's not a good idea to write extensions that modify their input
parameters, as in Example 3.  Instead, you should probably return multiple
values in an array and let the caller handle them (we'll do this in a later
example).  However, in order to better accommodate calling pre-existing C
routines, which often do modify their input parameters, this behavior is
tolerated.</p>
<p>
</p>
<h2><a name="example_4">EXAMPLE 4</a></h2>
<p>In this example, we'll now begin to write XSUBs that will interact with
pre-defined C libraries.  To begin with, we will build a small library of
our own, then let h2xs write our .pm and .xs files for us.</p>
<p>Create a new directory called Mytest2 at the same level as the directory
Mytest.  In the Mytest2 directory, create another directory called mylib,
and cd into that directory.</p>

⌨️ 快捷键说明

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