perlcall.html
来自「perl教程」· HTML 代码 · 共 1,026 行 · 第 1/5 页
HTML
1,026 行
<?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>perlcall - Perl calling conventions from C</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>perlcall - Perl calling conventions from C</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="#the_call__functions">THE CALL_ FUNCTIONS</a></li>
<li><a href="#flag_values">FLAG VALUES</a></li>
<ul>
<li><a href="#g_void">G_VOID</a></li>
<li><a href="#g_scalar">G_SCALAR</a></li>
<li><a href="#g_array">G_ARRAY</a></li>
<li><a href="#g_discard">G_DISCARD</a></li>
<li><a href="#g_noargs">G_NOARGS</a></li>
<li><a href="#g_eval">G_EVAL</a></li>
<li><a href="#g_keeperr">G_KEEPERR</a></li>
<li><a href="#determining_the_context">Determining the Context</a></li>
</ul>
<li><a href="#examples">EXAMPLES</a></li>
<ul>
<li><a href="#no_parameters__nothing_returned">No Parameters, Nothing returned</a></li>
<li><a href="#passing_parameters">Passing Parameters</a></li>
<li><a href="#returning_a_scalar">Returning a Scalar</a></li>
<li><a href="#returning_a_list_of_values">Returning a list of values</a></li>
<li><a href="#returning_a_list_in_a_scalar_context">Returning a list in a scalar context</a></li>
<li><a href="#returning_data_from_perl_via_the_parameter_list">Returning Data from Perl via the parameter list</a></li>
<li><a href="#using_g_eval">Using G_EVAL</a></li>
<li><a href="#using_g_keeperr">Using G_KEEPERR</a></li>
<li><a href="#using_call_sv">Using call_sv</a></li>
<li><a href="#using_call_argv">Using call_argv</a></li>
<li><a href="#using_call_method">Using call_method</a></li>
<li><a href="#using_gimme_v">Using GIMME_V</a></li>
<li><a href="#using_perl_to_dispose_of_temporaries">Using Perl to dispose of temporaries</a></li>
<li><a href="#strategies_for_storing_callback_context_information">Strategies for storing Callback Context Information</a></li>
<li><a href="#alternate_stack_manipulation">Alternate Stack Manipulation</a></li>
<li><a href="#creating_and_calling_an_anonymous_subroutine_in_c">Creating and calling an anonymous subroutine in C</a></li>
</ul>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#author">AUTHOR</a></li>
<li><a href="#date">DATE</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlcall - Perl calling conventions from C</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The purpose of this document is to show you how to call Perl subroutines
directly from C, i.e., how to write <em>callbacks</em>.</p>
<p>Apart from discussing the C interface provided by Perl for writing
callbacks the document uses a series of examples to show how the
interface actually works in practice. In addition some techniques for
coding callbacks are covered.</p>
<p>Examples where callbacks are necessary include</p>
<ul>
<li><strong><a name="item_an_error_handler">An Error Handler</a></strong>
<p>You have created an XSUB interface to an application's C API.</p>
<p>A fairly common feature in applications is to allow you to define a C
function that will be called whenever something nasty occurs. What we
would like is to be able to specify a Perl subroutine that will be
called instead.</p>
</li>
<li><strong><a name="item_an_event_driven_program">An Event Driven Program</a></strong>
<p>The classic example of where callbacks are used is when writing an
event driven program like for an X windows application. In this case
you register functions to be called whenever specific events occur,
e.g., a mouse button is pressed, the cursor moves into a window or a
menu item is selected.</p>
</li>
</ul>
<p>Although the techniques described here are applicable when embedding
Perl in a C program, this is not the primary goal of this document.
There are other details that must be considered and are specific to
embedding Perl. For details on embedding Perl in C refer to
<a href="../../lib/Pod/perlembed.html">the perlembed manpage</a>.</p>
<p>Before you launch yourself head first into the rest of this document,
it would be a good idea to have read the following two documents -
<a href="../../lib/Pod/perlxs.html">the perlxs manpage</a> and <a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>.</p>
<p>
</p>
<hr />
<h1><a name="the_call__functions">THE CALL_ FUNCTIONS</a></h1>
<p>Although this stuff is easier to explain using examples, you first need
be aware of a few important definitions.</p>
<p>Perl has a number of C functions that allow you to call Perl
subroutines. They are</p>
<pre>
<span class="variable">I32</span> <span class="variable">call_sv</span><span class="operator">(</span><span class="variable">SV</span><span class="operator">*</span> <span class="variable">sv</span><span class="operator">,</span> <span class="variable">I32</span> <span class="variable">flags</span><span class="operator">);</span>
<span class="variable">I32</span> <span class="variable">call_pv</span><span class="operator">(</span><span class="variable">char</span> <span class="variable">*subname</span><span class="operator">,</span> <span class="variable">I32</span> <span class="variable">flags</span><span class="operator">);</span>
<span class="variable">I32</span> <span class="variable">call_method</span><span class="operator">(</span><span class="variable">char</span> <span class="variable">*methname</span><span class="operator">,</span> <span class="variable">I32</span> <span class="variable">flags</span><span class="operator">);</span>
<span class="variable">I32</span> <span class="variable">call_argv</span><span class="operator">(</span><span class="variable">char</span> <span class="variable">*subname</span><span class="operator">,</span> <span class="variable">I32</span> <span class="variable">flags</span><span class="operator">,</span> <span class="variable">register</span> <span class="variable">char</span> <span class="operator">**</span><span class="variable">argv</span><span class="operator">);</span>
</pre>
<p>The key function is <em>call_sv</em>. All the other functions are
fairly simple wrappers which make it easier to call Perl subroutines in
special cases. At the end of the day they will all call <em>call_sv</em>
to invoke the Perl subroutine.</p>
<p>All the <em>call_*</em> functions have a <a href="../../lib/Pod/perlfunc.html#item_flags"><code>flags</code></a> parameter which is
used to pass a bit mask of options to Perl. This bit mask operates
identically for each of the functions. The settings available in the
bit mask are discussed in <a href="#flag_values">FLAG VALUES</a>.</p>
<p>Each of the functions will now be discussed in turn.</p>
<dl>
<dt><strong><a name="item_call_sv">call_sv</a></strong>
<dd>
<p><em>call_sv</em> takes two parameters, the first, <code>sv</code>, is an SV*.
This allows you to specify the Perl subroutine to be called either as a
C string (which has first been converted to an SV) or a reference to a
subroutine. The section, <em>Using call_sv</em>, shows how you can make
use of <em>call_sv</em>.</p>
</dd>
</li>
<dt><strong><a name="item_call_pv">call_pv</a></strong>
<dd>
<p>The function, <em>call_pv</em>, is similar to <em>call_sv</em> except it
expects its first parameter to be a C char* which identifies the Perl
subroutine you want to call, e.g., <a href="#item_call_pv"><code>call_pv("fred", 0)</code></a>. If the
subroutine you want to call is in another package, just include the
package name in the string, e.g., <code>"pkg::fred"</code>.</p>
</dd>
</li>
<dt><strong><a name="item_call_method">call_method</a></strong>
<dd>
<p>The function <em>call_method</em> is used to call a method from a Perl
class. The parameter <code>methname</code> corresponds to the name of the method
to be called. Note that the class that the method belongs to is passed
on the Perl stack rather than in the parameter list. This class can be
either the name of the class (for a static method) or a reference to an
object (for a virtual method). See <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a> for more information on
static and virtual methods and <a href="#using_call_method">Using call_method</a> for an example
of using <em>call_method</em>.</p>
</dd>
</li>
<dt><strong><a name="item_call_argv">call_argv</a></strong>
<dd>
<p><em>call_argv</em> calls the Perl subroutine specified by the C string
stored in the <code>subname</code> parameter. It also takes the usual <a href="../../lib/Pod/perlfunc.html#item_flags"><code>flags</code></a>
parameter. The final parameter, <code>argv</code>, consists of a NULL terminated
list of C strings to be passed as parameters to the Perl subroutine.
See <em>Using call_argv</em>.</p>
</dd>
</li>
</dl>
<p>All the functions return an integer. This is a count of the number of
items returned by the Perl subroutine. The actual items returned by the
subroutine are stored on the Perl stack.</p>
<p>As a general rule you should <em>always</em> check the return value from
these functions. Even if you are expecting only a particular number of
values to be returned from the Perl subroutine, there is nothing to
stop someone from doing something unexpected--don't say you haven't
been warned.</p>
<p>
</p>
<hr />
<h1><a name="flag_values">FLAG VALUES</a></h1>
<p>The <a href="../../lib/Pod/perlfunc.html#item_flags"><code>flags</code></a> parameter in all the <em>call_*</em> functions is a bit mask
which can consist of any combination of the symbols defined below,
OR'ed together.</p>
<p>
</p>
<h2><a name="g_void">G_VOID</a></h2>
<p>Calls the Perl subroutine in a void context.</p>
<p>This flag has 2 effects:</p>
<ol>
<li>
<p>It indicates to the subroutine being called that it is executing in
a void context (if it executes <em>wantarray</em> the result will be the
undefined value).</p>
</li>
<li>
<p>It ensures that nothing is actually returned from the subroutine.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?