perlxstut.html

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

HTML
907 行
字号
        <span class="operator">);</span>
</pre>
<p>The file Mytest.pm should start with something like this:</p>
<pre>
        <span class="keyword">package</span> <span class="variable">Mytest</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">use</span> <span class="variable">strict</span><span class="operator">;</span>
        <span class="keyword">use</span> <span class="variable">warnings</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">require</span> <span class="variable">Exporter</span><span class="operator">;</span>
        <span class="keyword">require</span> <span class="variable">DynaLoader</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">our</span> <span class="variable">@ISA</span> <span class="operator">=</span> <span class="string">qw(Exporter DynaLoader)</span><span class="operator">;</span>
        <span class="comment"># Items to export into callers namespace by default. Note: do not export</span>
        <span class="comment"># names by default without a very good reason. Use EXPORT_OK instead.</span>
        <span class="comment"># Do not simply export all your public functions/methods/constants.</span>
        <span class="keyword">our</span> <span class="variable">@EXPORT</span> <span class="operator">=</span> <span class="string">qw(
        </span>
</pre>
<pre>
        <span class="operator">);</span>
        <span class="keyword">our</span> <span class="variable">$VERSION</span> <span class="operator">=</span> <span class="string">'0.01'</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">bootstrap</span> <span class="variable">Mytest</span> <span class="variable">$VERSION</span><span class="operator">;</span>
</pre>
<pre>
        <span class="comment"># Preloaded methods go here.</span>
</pre>
<pre>
        <span class="comment"># Autoload methods go after __END__, and are processed by the autosplit program.</span>
</pre>
<pre>
        <span class="number">1</span><span class="operator">;</span>
        <span class="comment">__END__
        # Below is the stub of documentation for your module. You better edit it!
        </span>
</pre>
<p>The rest of the .pm file contains sample code for providing documentation for
the extension.</p>
<p>Finally, the Mytest.xs file should look something like this:</p>
<pre>
        <span class="comment">#include "EXTERN.h"</span>
        <span class="comment">#include "perl.h"</span>
        <span class="comment">#include "XSUB.h"</span>
</pre>
<pre>
        MODULE = Mytest         PACKAGE = Mytest</pre>
<p>Let's edit the .xs file by adding this to the end of the file:</p>
<pre>
        <span class="variable">void</span>
        <span class="variable">hello</span><span class="operator">()</span>
            <span class="variable">CODE</span><span class="operator">:</span>
                <span class="keyword">printf</span><span class="operator">(</span><span class="string">"Hello, world!\n"</span><span class="operator">);</span>
</pre>
<p>It is okay for the lines starting at the &quot;CODE:&quot; 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.</p>
<p>Now we'll run &quot;<code>perl Makefile.PL</code>&quot;.  This will create a real Makefile,
which make needs.  Its output looks something like:</p>
<pre>
        % perl Makefile.PL
        Checking if your kit is complete...
        Looks good
        Writing Makefile for Mytest
        %</pre>
<p>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):</p>
<pre>
        % make
        umask 0 &amp;&amp; cp Mytest.pm ./blib/Mytest.pm
        perl xsubpp -typemap typemap Mytest.xs &gt;Mytest.tc &amp;&amp; mv Mytest.tc Mytest.c
        Please specify prototyping behavior for Mytest.xs (see <a href="../../lib/Pod/perlxs.html">perlxs</a> manual)
        cc -c Mytest.c
        Running Mkbootstrap for Mytest ()
        chmod 644 Mytest.bs
        LD_RUN_PATH=&quot;&quot; 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
        Manifying ./blib/man3/Mytest.3
        %</pre>
<p>You can safely ignore the line about &quot;prototyping behavior&quot; - it is
explained in the section &quot;The PROTOTYPES: Keyword&quot; in <a href="../../lib/Pod/perlxs.html">the perlxs manpage</a>.</p>
<p>If you are on a Win32 system, and the build process fails with linker
errors for functions in the C library, check if your Perl is configured
to use PerlCRT (running <strong>perl -V:libc</strong> should show you if this is the
case).  If Perl is configured to use PerlCRT, you have to make sure
PerlCRT.lib is copied to the same location that msvcrt.lib lives in,
so that the compiler can find it on its own.  msvcrt.lib is usually
found in the Visual C compiler's lib directory (e.g. C:/DevStudio/VC/lib).</p>
<p>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:</p>
<pre>
        <span class="comment">#! /opt/perl5/bin/perl</span>
</pre>
<pre>
        <span class="keyword">use</span> <span class="variable">ExtUtils::testlib</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">use</span> <span class="variable">Mytest</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">Mytest::hello</span><span class="operator">();</span>
</pre>
<p>Now we make the script executable (<code>chmod +x hello</code>), run the script
and we should see the following output:</p>
<pre>
        % ./hello
        Hello, world!
        %</pre>
<p>
</p>
<h2><a name="example_2">EXAMPLE 2</a></h2>
<p>Now let's add to our extension a subroutine that will take a single numeric
argument as input and return 0 if the number is even or 1 if the number
is odd.</p>
<p>Add the following to the end of Mytest.xs:</p>
<pre>
        <span class="keyword">int</span>
        <span class="variable">is_even</span><span class="operator">(</span><span class="variable">input</span><span class="operator">)</span>
                <span class="keyword">int</span>     <span class="variable">input</span>
            <span class="variable">CODE</span><span class="operator">:</span>
                <span class="variable">RETVAL</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">input</span> <span class="operator">% </span><span class="number">2</span> <span class="operator">==</span> <span class="number">0</span><span class="operator">);</span>
            <span class="variable">OUTPUT</span><span class="operator">:</span>
                <span class="variable">RETVAL</span>
</pre>
<p>There does not need to be whitespace at the start of the &quot;<code>int input</code>&quot;
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 &quot;<a href="../../lib/Pod/perlfunc.html#item_int"><code>int</code></a>&quot; and &quot;<code>input</code>&quot;.</p>
<p>Now re-run make to rebuild our new shared library.</p>
<p>Now perform the same steps as before, generating a Makefile from the
Makefile.PL file, and running make.</p>
<p>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 &quot;ok&quot;
when the test is correct, &quot;not ok&quot; when it is not.  Change the print
statement in the BEGIN block to print &quot;1..4&quot;, and add the following code
to the end of the file:</p>
<pre>
        <span class="keyword">print</span> <span class="operator">&amp;</span><span class="variable">Mytest::is_even</span><span class="operator">(</span><span class="number">0</span><span class="operator">)</span> <span class="operator">==</span> <span class="number">1</span> <span class="operator">?</span> <span class="string">"ok 2"</span> <span class="operator">:</span> <span class="string">"not ok 2"</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="operator">&amp;</span><span class="variable">Mytest::is_even</span><span class="operator">(</span><span class="number">1</span><span class="operator">)</span> <span class="operator">==</span> <span class="number">0</span> <span class="operator">?</span> <span class="string">"ok 3"</span> <span class="operator">:</span> <span class="string">"not ok 3"</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="operator">&amp;</span><span class="variable">Mytest::is_even</span><span class="operator">(</span><span class="number">2</span><span class="operator">)</span> <span class="operator">==</span> <span class="number">1</span> <span class="operator">?</span> <span class="string">"ok 4"</span> <span class="operator">:</span> <span class="string">"not ok 4"</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>We will be calling the test script through the command &quot;<code>make test</code>&quot;.  You
should see output that looks something like this:</p>
<pre>
        % make test
        PERL_DL_NONLAZY=1 /opt/perl5.004/bin/perl (lots of -I arguments) test.pl
        1..4
        ok 1
        ok 2
        ok 3
        ok 4
        %</pre>
<p>
</p>
<h2><a name="what_has_gone_on">What has gone on?</a></h2>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>Generating the Makefile and running <code>make</code> created a directory called blib
(which stands for &quot;build library&quot;) 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.</p>
<p>Invoking the test script via &quot;<code>make test</code>&quot; did something very important.
It invoked perl with all those <code>-I</code> arguments so that it could find the
various files that are part of the extension.  It is <em>very</em> important that
while you are still testing extensions that you use &quot;<code>make test</code>&quot;.  If you

⌨️ 快捷键说明

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