perlcall.html

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

HTML
1,026 行
字号
    </span><span class="operator">{</span>
        <span class="keyword">print</span> <span class="string">"UID is $&lt;\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>and here is a C function to call it</p>
<pre>
    <span class="variable">static</span> <span class="variable">void</span>
    <span class="variable">call_PrintUID</span><span class="operator">()</span>
    <span class="operator">{</span>
        <span class="variable">dSP</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">PUSHMARK</span><span class="operator">(</span><span class="variable">SP</span><span class="operator">);</span>
        <span class="variable">call_pv</span><span class="operator">(</span><span class="string">"PrintUID"</span><span class="operator">,</span> <span class="variable">G_DISCARD</span><span class="operator">|</span><span class="variable">G_NOARGS</span><span class="operator">);</span>
            <span class="operator">}</span>
</pre>
<p>Simple, eh.</p>
<p>A few points to note about this example.</p>
<ol>
<li>
<p>Ignore <code>dSP</code> and <code>PUSHMARK(SP)</code> for now. They will be discussed in
the next example.</p>
</li>
<li>
<p>We aren't passing any parameters to <em>PrintUID</em> so G_NOARGS can be
specified.</p>
</li>
<li>
<p>We aren't interested in anything returned from <em>PrintUID</em>, so
G_DISCARD is specified. Even if <em>PrintUID</em> was changed to
return some value(s), having specified G_DISCARD will mean that they
will be wiped by the time control returns from <em>call_pv</em>.</p>
</li>
<li>
<p>As <em>call_pv</em> is being used, the Perl subroutine is specified as a
C string. In this case the subroutine name has been 'hard-wired' into the
code.</p>
</li>
<li>
<p>Because we specified G_DISCARD, it is not necessary to check the value
returned from <em>call_pv</em>. It will always be 0.</p>
</li>
</ol>
<p>
</p>
<h2><a name="passing_parameters">Passing Parameters</a></h2>
<p>Now let's make a slightly more complex example. This time we want to
call a Perl subroutine, <code>LeftString</code>, which will take 2 parameters--a
string ($s) and an integer ($n).  The subroutine will simply
print the first $n characters of the string.</p>
<p>So the Perl subroutine would look like this</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> LeftString
    </span><span class="operator">{</span>
        <span class="keyword">my</span><span class="operator">(</span><span class="variable">$s</span><span class="operator">,</span> <span class="variable">$n</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="keyword">substr</span><span class="operator">(</span><span class="variable">$s</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="variable">$n</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>The C function required to call <em>LeftString</em> would look like this.</p>
<pre>
    <span class="variable">static</span> <span class="variable">void</span>
    <span class="variable">call_LeftString</span><span class="operator">(</span><span class="variable">a</span><span class="operator">,</span> <span class="variable">b</span><span class="operator">)</span>
    <span class="variable">char</span> <span class="operator">*</span> <span class="variable">a</span><span class="operator">;</span>
    <span class="keyword">int</span> <span class="variable">b</span><span class="operator">;</span>
    <span class="operator">{</span>
        <span class="variable">dSP</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">ENTER</span><span class="operator">;</span>
        <span class="variable">SAVETMPS</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">PUSHMARK</span><span class="operator">(</span><span class="variable">SP</span><span class="operator">);</span>
        <span class="variable">XPUSHs</span><span class="operator">(</span><span class="variable">sv_2mortal</span><span class="operator">(</span><span class="variable">newSVpv</span><span class="operator">(</span><span class="variable">a</span><span class="operator">,</span> <span class="number">0</span><span class="operator">)));</span>
        <span class="variable">XPUSHs</span><span class="operator">(</span><span class="variable">sv_2mortal</span><span class="operator">(</span><span class="variable">newSViv</span><span class="operator">(</span><span class="variable">b</span><span class="operator">)));</span>
        <span class="variable">PUTBACK</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">call_pv</span><span class="operator">(</span><span class="string">"LeftString"</span><span class="operator">,</span> <span class="variable">G_DISCARD</span><span class="operator">);</span>
</pre>
<pre>
        <span class="variable">FREETMPS</span><span class="operator">;</span>
        <span class="variable">LEAVE</span><span class="operator">;</span>
            <span class="operator">}</span>
</pre>
<p>Here are a few notes on the C function <em>call_LeftString</em>.</p>
<ol>
<li>
<p>Parameters are passed to the Perl subroutine using the Perl stack.
This is the purpose of the code beginning with the line <code>dSP</code> and
ending with the line <code>PUTBACK</code>.  The <code>dSP</code> declares a local copy
of the stack pointer.  This local copy should <strong>always</strong> be accessed
as <code>SP</code>.</p>
</li>
<li>
<p>If you are going to put something onto the Perl stack, you need to know
where to put it. This is the purpose of the macro <code>dSP</code>--it declares
and initializes a <em>local</em> copy of the Perl stack pointer.</p>
<p>All the other macros which will be used in this example require you to
have used this macro.</p>
<p>The exception to this rule is if you are calling a Perl subroutine
directly from an XSUB function. In this case it is not necessary to
use the <code>dSP</code> macro explicitly--it will be declared for you
automatically.</p>
</li>
<li>
<p>Any parameters to be pushed onto the stack should be bracketed by the
<code>PUSHMARK</code> and <code>PUTBACK</code> macros.  The purpose of these two macros, in
this context, is to count the number of parameters you are
pushing automatically.  Then whenever Perl is creating the <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> array for the
subroutine, it knows how big to make it.</p>
<p>The <code>PUSHMARK</code> macro tells Perl to make a mental note of the current
stack pointer. Even if you aren't passing any parameters (like the
example shown in the section <em>No Parameters, Nothing returned</em>) you
must still call the <code>PUSHMARK</code> macro before you can call any of the
<em>call_*</em> functions--Perl still needs to know that there are no
parameters.</p>
<p>The <code>PUTBACK</code> macro sets the global copy of the stack pointer to be
the same as our local copy. If we didn't do this <em>call_pv</em>
wouldn't know where the two parameters we pushed were--remember that
up to now all the stack pointer manipulation we have done is with our
local copy, <em>not</em> the global copy.</p>
</li>
<li>
<p>Next, we come to XPUSHs. This is where the parameters actually get
pushed onto the stack. In this case we are pushing a string and an
integer.</p>
<p>See <a href="../../lib/Pod/perlguts.html#xsubs_and_the_argument_stack">XSUBs and the Argument Stack in the perlguts manpage</a> for details
on how the XPUSH macros work.</p>
</li>
<li>
<p>Because we created temporary values (by means of <code>sv_2mortal()</code> calls)
we will have to tidy up the Perl stack and dispose of mortal SVs.</p>
<p>This is the purpose of</p>
<pre>
    <span class="variable">ENTER</span><span class="operator">;</span>
    <span class="variable">SAVETMPS</span><span class="operator">;</span>
</pre>
<p>at the start of the function, and</p>
<pre>
    <span class="variable">FREETMPS</span><span class="operator">;</span>
    <span class="variable">LEAVE</span><span class="operator">;</span>
</pre>
<p>at the end. The <code>ENTER</code>/<code>SAVETMPS</code> pair creates a boundary for any
temporaries we create.  This means that the temporaries we get rid of
will be limited to those which were created after these calls.</p>
<p>The <code>FREETMPS</code>/<code>LEAVE</code> pair will get rid of any values returned by
the Perl subroutine (see next example), plus it will also dump the
mortal SVs we have created.  Having <code>ENTER</code>/<code>SAVETMPS</code> at the
beginning of the code makes sure that no other mortals are destroyed.</p>
<p>Think of these macros as working a bit like using <code>{</code> and <code>}</code> in Perl
to limit the scope of local variables.</p>
<p>See the section <em>Using Perl to dispose of temporaries</em> for details of
an alternative to using these macros.</p>
</li>
<li>
<p>Finally, <em>LeftString</em> can now be called via the <em>call_pv</em> function.
The only flag specified this time is G_DISCARD. Because we are passing
2 parameters to the Perl subroutine this time, we have not specified
G_NOARGS.</p>
</li>
</ol>
<p>
</p>
<h2><a name="returning_a_scalar">Returning a Scalar</a></h2>
<p>Now for an example of dealing with the items returned from a Perl
subroutine.</p>
<p>Here is a Perl subroutine, <em>Adder</em>, that takes 2 integer parameters
and simply returns their sum.</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> Adder
    </span><span class="operator">{</span>
        <span class="keyword">my</span><span class="operator">(</span><span class="variable">$a</span><span class="operator">,</span> <span class="variable">$b</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">+</span> <span class="variable">$b</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>Because we are now concerned with the return value from <em>Adder</em>, the C
function required to call it is now a bit more complex.</p>
<pre>
    <span class="variable">static</span> <span class="variable">void</span>
    <span class="variable">call_Adder</span><span class="operator">(</span><span class="variable">a</span><span class="operator">,</span> <span class="variable">b</span><span class="operator">)</span>
    <span class="keyword">int</span> <span class="variable">a</span><span class="operator">;</span>
    <span class="keyword">int</span> <span class="variable">b</span><span class="operator">;</span>
    <span class="operator">{</span>
        <span class="variable">dSP</span><span class="operator">;</span>
        <span class="keyword">int</span> <span class="variable">count</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">ENTER</span><span class="operator">;</span>
        <span class="variable">SAVETMPS</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">PUSHMARK</span><span class="operator">(</span><span class="variable">SP</span><span class="operator">);</span>
        <span class="variable">XPUSHs</span><span class="operator">(</span><span class="variable">sv_2mortal</span><span class="operator">(</span><span class="variable">newSViv</span><span class="operator">(</span><span class="variable">a</span><span class="operator">)));</span>
        <span class="variable">XPUSHs</span><span class="operator">(</span><span class="variable">sv_2mortal</span><span class="operator">(</span><span class="variable">newSViv</span><span class="operator">(</span><span class="variable">b</span><span class="operator">)));</span>
        <span class="variable">PUTBACK</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">count</span> <span class="operator">=</span> <span class="variable">call_pv</span><span class="operator">(</span><span class="string">"Adder"</span><span class="operator">,</span> <span class="variable">G_SCALAR</span><span class="operator">);</span>
</pre>
<pre>
        <span class="variable">SPAGAIN</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">if</span> <span class="operator">(</span><span class="variable">count</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">"Big trouble\n"</span><span class="operator">);</span>

⌨️ 快捷键说明

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