perlcall.html

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

HTML
1,026 行
字号
</li>
</ol>
<p>The value returned by the <em>call_*</em> function indicates how many
items have been returned by the Perl subroutine - in this case it will
be 0.</p>
<p>
</p>
<h2><a name="g_scalar">G_SCALAR</a></h2>
<p>Calls the Perl subroutine in a scalar context.  This is the default
context flag setting for all the <em>call_*</em> functions.</p>
<p>This flag has 2 effects:</p>
<ol>
<li>
<p>It indicates to the subroutine being called that it is executing in a
scalar context (if it executes <em>wantarray</em> the result will be false).</p>
</li>
<li>
<p>It ensures that only a scalar is actually returned from the subroutine.
The subroutine can, of course,  ignore the <em>wantarray</em> and return a
list anyway. If so, then only the last element of the list will be
returned.</p>
</li>
</ol>
<p>The value returned by the <em>call_*</em> function indicates how many
items have been returned by the Perl subroutine - in this case it will
be either 0 or 1.</p>
<p>If 0, then you have specified the G_DISCARD flag.</p>
<p>If 1, then the item actually returned by the Perl subroutine will be
stored on the Perl stack - the section <em>Returning a Scalar</em> shows how
to access this value on the stack.  Remember that regardless of how
many items the Perl subroutine returns, only the last one will be
accessible from the stack - think of the case where only one value is
returned as being a list with only one element.  Any other items that
were returned will not exist by the time control returns from the
<em>call_*</em> function.  The section <em>Returning a list in a scalar
context</em> shows an example of this behavior.</p>
<p>
</p>
<h2><a name="g_array">G_ARRAY</a></h2>
<p>Calls the Perl subroutine in a list context.</p>
<p>As with G_SCALAR, this flag has 2 effects:</p>
<ol>
<li>
<p>It indicates to the subroutine being called that it is executing in a
list context (if it executes <em>wantarray</em> the result will be true).</p>
</li>
<li>
<p>It ensures that all items returned from the subroutine will be
accessible when control returns from the <em>call_*</em> function.</p>
</li>
</ol>
<p>The value returned by the <em>call_*</em> function indicates how many
items have been returned by the Perl subroutine.</p>
<p>If 0, then you have specified the G_DISCARD flag.</p>
<p>If not 0, then it will be a count of the number of items returned by
the subroutine. These items will be stored on the Perl stack.  The
section <em>Returning a list of values</em> gives an example of using the
G_ARRAY flag and the mechanics of accessing the returned items from the
Perl stack.</p>
<p>
</p>
<h2><a name="g_discard">G_DISCARD</a></h2>
<p>By default, the <em>call_*</em> functions place the items returned from
by the Perl subroutine on the stack.  If you are not interested in
these items, then setting this flag will make Perl get rid of them
automatically for you.  Note that it is still possible to indicate a
context to the Perl subroutine by using either G_SCALAR or G_ARRAY.</p>
<p>If you do not set this flag then it is <em>very</em> important that you make
sure that any temporaries (i.e., parameters passed to the Perl
subroutine and values returned from the subroutine) are disposed of
yourself.  The section <em>Returning a Scalar</em> gives details of how to
dispose of these temporaries explicitly and the section <em>Using Perl to
dispose of temporaries</em> discusses the specific circumstances where you
can ignore the problem and let Perl deal with it for you.</p>
<p>
</p>
<h2><a name="g_noargs">G_NOARGS</a></h2>
<p>Whenever a Perl subroutine is called using one of the <em>call_*</em>
functions, it is assumed by default that parameters are to be passed to
the subroutine.  If you are not passing any parameters to the Perl
subroutine, you can save a bit of time by setting this flag.  It has
the effect of not creating the <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> array for the Perl subroutine.</p>
<p>Although the functionality provided by this flag may seem
straightforward, it should be used only if there is a good reason to do
so.  The reason for being cautious is that even if you have specified
the G_NOARGS flag, it is still possible for the Perl subroutine that
has been called to think that you have passed it parameters.</p>
<p>In fact, what can happen is that the Perl subroutine you have called
can access the <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> array from a previous Perl subroutine.  This will
occur when the code that is executing the <em>call_*</em> function has
itself been called from another Perl subroutine. The code below
illustrates this</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> fred
      </span><span class="operator">{</span> <span class="keyword">print</span> <span class="string">"@_\n"</span>  <span class="operator">}</span>
</pre>
<pre>
    <span class="keyword">sub</span><span class="variable"> joe
      </span><span class="operator">{</span> <span class="operator">&amp;</span><span class="variable">fred</span> <span class="operator">}</span>
</pre>
<pre>
    <span class="operator">&amp;</span><span class="variable">joe</span><span class="operator">(</span><span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span><span class="operator">);</span>
</pre>
<p>This will print</p>
<pre>
    1 2 3</pre>
<p>What has happened is that <code>fred</code> accesses the <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> array which
belongs to <code>joe</code>.</p>
<p>
</p>
<h2><a name="g_eval">G_EVAL</a></h2>
<p>It is possible for the Perl subroutine you are calling to terminate
abnormally, e.g., by calling <em>die</em> explicitly or by not actually
existing.  By default, when either of these events occurs, the
process will terminate immediately.  If you want to trap this
type of event, specify the G_EVAL flag.  It will put an <em>eval { }</em>
around the subroutine call.</p>
<p>Whenever control returns from the <em>call_*</em> function you need to
check the <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a> variable as you would in a normal Perl script.</p>
<p>The value returned from the <em>call_*</em> function is dependent on
what other flags have been specified and whether an error has
occurred.  Here are all the different cases that can occur:</p>
<ul>
<li>
<p>If the <em>call_*</em> function returns normally, then the value
returned is as specified in the previous sections.</p>
</li>
<li>
<p>If G_DISCARD is specified, the return value will always be 0.</p>
</li>
<li>
<p>If G_ARRAY is specified <em>and</em> an error has occurred, the return value
will always be 0.</p>
</li>
<li>
<p>If G_SCALAR is specified <em>and</em> an error has occurred, the return value
will be 1 and the value on the top of the stack will be <em>undef</em>. This
means that if you have already detected the error by checking <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a> and
you want the program to continue, you must remember to pop the <em>undef</em>
from the stack.</p>
</li>
</ul>
<p>See <em>Using G_EVAL</em> for details on using G_EVAL.</p>
<p>
</p>
<h2><a name="g_keeperr">G_KEEPERR</a></h2>
<p>You may have noticed that using the G_EVAL flag described above will
<strong>always</strong> clear the <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a> variable and set it to a string describing
the error iff there was an error in the called code.  This unqualified
resetting of <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a> can be problematic in the reliable identification of
errors using the <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval {}</code></a> mechanism, because the possibility exists
that perl will call other code (end of block processing code, for
example) between the time the error causes <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a> to be set within
<a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval {}</code></a>, and the subsequent statement which checks for the value of
<a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a> gets executed in the user's script.</p>
<p>This scenario will mostly be applicable to code that is meant to be
called from within destructors, asynchronous callbacks, signal
handlers, <code>__DIE__</code> or <code>__WARN__</code> hooks, and <a href="../../lib/Pod/perlfunc.html#item_tie"><code>tie</code></a> functions.  In
such situations, you will not want to clear <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a> at all, but simply to
append any new errors to any existing value of <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a>.</p>
<p>The G_KEEPERR flag is meant to be used in conjunction with G_EVAL in
<em>call_*</em> functions that are used to implement such code.  This flag
has no effect when G_EVAL is not used.</p>
<p>When G_KEEPERR is used, any errors in the called code will be prefixed
with the string &quot;\t(in cleanup)&quot;, and appended to the current value
of <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a>.  an error will not be appended if that same error string is
already at the end of <a href="../../lib/Pod/perlvar.html#item___"><code>$@</code></a>.</p>
<p>In addition, a warning is generated using the appended string. This can be
disabled using <code>no warnings 'misc'</code>.</p>
<p>The G_KEEPERR flag was introduced in Perl version 5.002.</p>
<p>See <em>Using G_KEEPERR</em> for an example of a situation that warrants the
use of this flag.</p>
<p>
</p>
<h2><a name="determining_the_context">Determining the Context</a></h2>
<p>As mentioned above, you can determine the context of the currently
executing subroutine in Perl with <em>wantarray</em>.  The equivalent test
can be made in C by using the <code>GIMME_V</code> macro, which returns
<code>G_ARRAY</code> if you have been called in a list context, <code>G_SCALAR</code> if
in a scalar context, or <code>G_VOID</code> if in a void context (i.e. the
return value will not be used).  An older version of this macro is
called <code>GIMME</code>; in a void context it returns <code>G_SCALAR</code> instead of
<code>G_VOID</code>.  An example of using the <code>GIMME_V</code> macro is shown in
section <em>Using GIMME_V</em>.</p>
<p>
</p>
<hr />
<h1><a name="examples">EXAMPLES</a></h1>
<p>Enough of the definition talk, let's have a few examples.</p>
<p>Perl provides many macros to assist in accessing the Perl stack.
Wherever possible, these macros should always be used when interfacing
to Perl internals.  We hope this should make the code less vulnerable
to any changes made to Perl in the future.</p>
<p>Another point worth noting is that in the first series of examples I
have made use of only the <em>call_pv</em> function.  This has been done
to keep the code simpler and ease you into the topic.  Wherever
possible, if the choice is between using <em>call_pv</em> and
<em>call_sv</em>, you should always try to use <em>call_sv</em>.  See
<em>Using call_sv</em> for details.</p>
<p>
</p>
<h2><a name="no_parameters__nothing_returned">No Parameters, Nothing returned</a></h2>
<p>This first trivial example will call a Perl subroutine, <em>PrintUID</em>, to
print out the UID of the process.</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> PrintUID

⌨️ 快捷键说明

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