perlxstut.html
来自「perl教程」· HTML 代码 · 共 907 行 · 第 1/5 页
HTML
907 行
twice in the generated .xs file: once in the first part, as a static C
function, then another time in the second part, when an XSUB interface to
this static C function is defined.</p>
<p>This is quite typical for .xs files: usually the .xs file provides
an interface to an existing C function. Then this C function is defined
somewhere (either in an external library, or in the first part of .xs file),
and a Perl interface to this function (i.e. "Perl glue") is described in the
second part of .xs file. The situation in <a href="#example_1">EXAMPLE 1</a>, <a href="#example_2">EXAMPLE 2</a>,
and <a href="#example_3">EXAMPLE 3</a>, when all the work is done inside the "Perl glue", is
somewhat of an exception rather than the rule.</p>
<p>
</p>
<h2><a name="getting_the_fat_out_of_xsubs">Getting the fat out of XSUBs</a></h2>
<p>In <a href="#example_4">EXAMPLE 4</a> the second part of .xs file contained the following
description of an XSUB:</p>
<pre>
double
foo(a,b,c)
int a
long b
const char * c
OUTPUT:
RETVAL</pre>
<p>Note that in contrast with <a href="#example_1">EXAMPLE 1</a>, <a href="#example_2">EXAMPLE 2</a> and <a href="#example_3">EXAMPLE 3</a>,
this description does not contain the actual <em>code</em> for what is done
is done during a call to Perl function foo(). To understand what is going
on here, one can add a CODE section to this XSUB:</p>
<pre>
<span class="variable">double</span>
<span class="variable">foo</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">c</span><span class="operator">)</span>
<span class="keyword">int</span> <span class="variable">a</span>
<span class="variable">long</span> <span class="variable">b</span>
<span class="variable">const</span> <span class="variable">char</span> <span class="operator">*</span> <span class="variable">c</span>
<span class="variable">CODE</span><span class="operator">:</span>
<span class="variable">RETVAL</span> <span class="operator">=</span> <span class="variable">foo</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">c</span><span class="operator">);</span>
<span class="variable">OUTPUT</span><span class="operator">:</span>
<span class="variable">RETVAL</span>
</pre>
<p>However, these two XSUBs provide almost identical generated C code: <strong>xsubpp</strong>
compiler is smart enough to figure out the <code>CODE:</code> section from the first
two lines of the description of XSUB. What about <code>OUTPUT:</code> section? In
fact, that is absolutely the same! The <code>OUTPUT:</code> section can be removed
as well, <em>as far as <code>CODE:</code> section or <code>PPCODE:</code> section</em> is not
specified: <strong>xsubpp</strong> can see that it needs to generate a function call
section, and will autogenerate the OUTPUT section too. Thus one can
shortcut the XSUB to become:</p>
<pre>
double
foo(a,b,c)
int a
long b
const char * c</pre>
<p>Can we do the same with an XSUB</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>of <a href="#example_2">EXAMPLE 2</a>? To do this, one needs to define a C function <code>int
is_even(int input)</code>. As we saw in <a href="#anatomy_of__xs_file">Anatomy of .xs file</a>, a proper place
for this definition is in the first part of .xs file. In fact a C function</p>
<pre>
<span class="keyword">int</span>
<span class="variable">is_even</span><span class="operator">(</span><span class="keyword">int</span> <span class="variable">arg</span><span class="operator">)</span>
<span class="operator">{</span>
<span class="keyword">return</span> <span class="operator">(</span><span class="variable">arg</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="operator">}</span>
</pre>
<p>is probably overkill for this. Something as simple as a <code>#define</code> will
do too:</p>
<pre>
<span class="comment">#define is_even(arg) ((arg) % 2 == 0)</span>
</pre>
<p>After having this in the first part of .xs file, the "Perl glue" part becomes
as simple as</p>
<pre>
int
is_even(input)
int input</pre>
<p>This technique of separation of the glue part from the workhorse part has
obvious tradeoffs: if you want to change a Perl interface, you need to
change two places in your code. However, it removes a lot of clutter,
and makes the workhorse part independent from idiosyncrasies of Perl calling
convention. (In fact, there is nothing Perl-specific in the above description,
a different version of <strong>xsubpp</strong> might have translated this to TCL glue or
Python glue as well.)</p>
<p>
</p>
<h2><a name="more_about_xsub_arguments">More about XSUB arguments</a></h2>
<p>With the completion of Example 4, we now have an easy way to simulate some
real-life libraries whose interfaces may not be the cleanest in the world.
We shall now continue with a discussion of the arguments passed to the
<strong>xsubpp</strong> compiler.</p>
<p>When you specify arguments to routines in the .xs file, you are really
passing three pieces of information for each argument listed. The first
piece is the order of that argument relative to the others (first, second,
etc). The second is the type of argument, and consists of the type
declaration of the argument (e.g., int, char*, etc). The third piece is
the calling convention for the argument in the call to the library function.</p>
<p>While Perl passes arguments to functions by reference,
C passes arguments by value; to implement a C function which modifies data
of one of the "arguments", the actual argument of this C function would be
a pointer to the data. Thus two C functions with declarations</p>
<pre>
<span class="keyword">int</span> <span class="variable">string_length</span><span class="operator">(</span><span class="variable">char</span> <span class="variable">*s</span><span class="operator">);</span>
<span class="keyword">int</span> <span class="variable">upper_case_char</span><span class="operator">(</span><span class="variable">char</span> <span class="variable">*cp</span><span class="operator">);</span>
</pre>
<p>may have completely different semantics: the first one may inspect an array
of chars pointed by s, and the second one may immediately dereference <code>cp</code>
and manipulate <code>*cp</code> only (using the return value as, say, a success
indicator). From Perl one would use these functions in
a completely different manner.</p>
<p>One conveys this info to <strong>xsubpp</strong> by replacing <code>*</code> before the
argument by <code>&</code>. <code>&</code> means that the argument should be passed to a library
function by its address. The above two function may be XSUB-ified as</p>
<pre>
int
string_length(s)
char * s</pre>
<pre>
int
upper_case_char(cp)
char &cp</pre>
<p>For example, consider:</p>
<pre>
int
foo(a,b)
char &a
char * b</pre>
<p>The first Perl argument to this function would be treated as a char and assigned
to the variable a, and its address would be passed into the function foo.
The second Perl argument would be treated as a string pointer and assigned to the
variable b. The <em>value</em> of b would be passed into the function foo. The
actual call to the function foo that <strong>xsubpp</strong> generates would look like this:</p>
<pre>
<span class="variable">foo</span><span class="operator">(&</span><span class="variable">a</span><span class="operator">,</span> <span class="variable">b</span><span class="operator">);</span>
</pre>
<p><strong>xsubpp</strong> will parse the following function argument lists identically:</p>
<pre>
char &a
char&a
char & a</pre>
<p>However, to help ease understanding, it is suggested that you place a "&"
next to the variable name and away from the variable type), and place a
"*" near the variable type, but away from the variable name (as in the
call to foo above). By doing so, it is easy to understand exactly what
will be passed to the C function -- it will be whatever is in the "last
column".</p>
<p>You should take great pains to try to pass the function the type of variable
it wants, when possible. It will save you a lot of trouble in the long run.</p>
<p>
</p>
<h2><a name="the_argument_stack">The Argument Stack</a></h2>
<p>If we look at any of the C code generated by any of the examples except
example 1, you will notice a number of references to ST(n), where n is
usually 0. "ST" is actually a macro that points to the n'th argument
on the argument stack. <code>ST(0)</code> is thus the first argument on the stack and
therefore the first argument passed to the XSUB, <code>ST(1)</code> is the second
argument, and so on.</p>
<p>When you list the arguments to the XSUB in the .xs file, that tells <strong>xsubpp</strong>
which argument corresponds to which of the argument stack (i.e., the first
one listed is the first argument, and so on). You invite disaster if you
do not list them in the same order as the function expects them.</p>
<p>The actual values on the argument stack are pointers to the values passed
in. When an argument is listed as being an OUTPUT value, its corresponding
value on the stack (i.e., <code>ST(0)</code> if it was the first argument) is changed.
You can verify this by looking at the C code generated for Example 3.
The code for the <code>round()</code> XSUB routine contains lines that look like this:</p>
<pre>
<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">/* Round the contents of the variable arg */</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>
</pre>
<p>The arg variable is initially set by taking the value from ST(0), then is
stored back into <code>ST(0)</code
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?