perlsub.html
来自「perl教程」· HTML 代码 · 共 734 行 · 第 1/5 页
HTML
734 行
<?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>perlsub - Perl subroutines</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>perlsub - Perl subroutines</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#private_variables_via_my__">Private Variables via <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a></a></li>
<li><a href="#persistent_private_variables">Persistent Private Variables</a></li>
<li><a href="#temporary_values_via_local__">Temporary Values via <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a></a></li>
<ul>
<li><a href="#grammatical_note_on_local__">Grammatical note on <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a></a></li>
<li><a href="#localization_of_special_variables">Localization of special variables</a></li>
<li><a href="#localization_of_globs">Localization of globs</a></li>
<li><a href="#localization_of_elements_of_composite_types">Localization of elements of composite types</a></li>
</ul>
<li><a href="#lvalue_subroutines">Lvalue subroutines</a></li>
<li><a href="#passing_symbol_table_entries__typeglobs_">Passing Symbol Table Entries (typeglobs)</a></li>
<li><a href="#when_to_still_use_local__">When to Still Use <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a></a></li>
<li><a href="#pass_by_reference">Pass by Reference</a></li>
<li><a href="#prototypes">Prototypes</a></li>
<li><a href="#constant_functions">Constant Functions</a></li>
<li><a href="#overriding_builtin_functions">Overriding Built-in Functions</a></li>
<li><a href="#autoloading">Autoloading</a></li>
<li><a href="#subroutine_attributes">Subroutine Attributes</a></li>
</ul>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlsub - Perl subroutines</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<p>To declare subroutines:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> NAME</span><span class="operator">;</span> <span class="comment"># A "forward" declaration.</span>
<span class="keyword">sub</span><span class="variable"> NAME</span><span class="operator">(</span>PROTO<span class="operator">);</span> <span class="comment"># ditto, but with prototypes</span>
<span class="keyword">sub</span><span class="variable"> NAME : ATTRS</span><span class="operator">;</span> <span class="comment"># with attributes</span>
<span class="keyword">sub</span><span class="variable"> NAME</span><span class="operator">(</span>PROTO<span class="operator">)</span> <span class="operator">:</span> <span class="variable">ATTRS</span><span class="operator">;</span> <span class="comment"># with attributes and prototypes</span>
</pre>
<pre>
sub NAME BLOCK # A declaration and a definition.
sub NAME(PROTO) BLOCK # ditto, but with prototypes
sub NAME : ATTRS BLOCK # with attributes
sub NAME(PROTO) : ATTRS BLOCK # with prototypes and attributes</pre>
<p>To define an anonymous subroutine at runtime:</p>
<pre>
<span class="variable">$subref</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> BLOCK</span><span class="operator">;</span> <span class="comment"># no proto</span>
<span class="variable">$subref</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">(</span>PROTO<span class="operator">)</span> <span class="variable">BLOCK</span><span class="operator">;</span> <span class="comment"># with proto</span>
<span class="variable">$subref</span> <span class="operator">=</span> <span class="variable">sub</span> <span class="operator">:</span> <span class="variable">ATTRS</span> <span class="variable">BLOCK</span><span class="operator">;</span> <span class="comment"># with attributes</span>
<span class="variable">$subref</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">(</span>PROTO<span class="operator">)</span> <span class="operator">:</span> <span class="variable">ATTRS</span> <span class="variable">BLOCK</span><span class="operator">;</span> <span class="comment"># with proto and attributes</span>
</pre>
<p>To import subroutines:</p>
<pre>
<span class="keyword">use</span> <span class="variable">MODULE</span> <span class="string">qw(NAME1 NAME2 NAME3)</span><span class="operator">;</span>
</pre>
<p>To call subroutines:</p>
<pre>
<span class="variable">NAME</span><span class="operator">(</span><span class="variable">LIST</span><span class="operator">);</span> <span class="comment"># & is optional with parentheses.</span>
<span class="variable">NAME</span> <span class="variable">LIST</span><span class="operator">;</span> <span class="comment"># Parentheses optional if predeclared/imported.</span>
<span class="operator">&</span><span class="variable">NAME</span><span class="operator">(</span><span class="variable">LIST</span><span class="operator">);</span> <span class="comment"># Circumvent prototypes.</span>
<span class="operator">&</span><span class="variable">NAME</span><span class="operator">;</span> <span class="comment"># Makes current @_ visible to called subroutine.</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Like many languages, Perl provides for user-defined subroutines.
These may be located anywhere in the main program, loaded in from
other files via the <a href="../../lib/Pod/perlfunc.html#item_do"><code>do</code></a>, <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a>, or <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> keywords, or
generated on the fly using <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a> or anonymous subroutines.
You can even call a function indirectly using a variable containing
its name or a CODE reference.</p>
<p>The Perl model for function call and return values is simple: all
functions are passed as parameters one single flat list of scalars, and
all functions likewise return to their caller one single flat list of
scalars. Any arrays or hashes in these call and return lists will
collapse, losing their identities--but you may always use
pass-by-reference instead to avoid this. Both call and return lists may
contain as many or as few scalar elements as you'd like. (Often a
function without an explicit return statement is called a subroutine, but
there's really no difference from Perl's perspective.)</p>
<p>Any arguments passed in show up in the array <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a>. Therefore, if
you called a function with two arguments, those would be stored in
<code>$_[0]</code> and <code>$_[1]</code>. The array <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> is a local array, but its
elements are aliases for the actual scalar parameters. In particular,
if an element <code>$_[0]</code> is updated, the corresponding argument is
updated (or an error occurs if it is not updatable). If an argument
is an array or hash element which did not exist when the function
was called, that element is created only when (and if) it is modified
or a reference to it is taken. (Some earlier versions of Perl
created the element whether or not the element was assigned to.)
Assigning to the whole array <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> removes that aliasing, and does
not update any arguments.</p>
<p>A <a href="../../lib/Pod/perlfunc.html#item_return"><code>return</code></a> statement may be used to exit a subroutine, optionally
specifying the returned value, which will be evaluated in the
appropriate context (list, scalar, or void) depending on the context of
the subroutine call. If you specify no return value, the subroutine
returns an empty list in list context, the undefined value in scalar
context, or nothing in void context. If you return one or more
aggregates (arrays and hashes), these will be flattened together into
one large indistinguishable list.</p>
<p>If no <a href="../../lib/Pod/perlfunc.html#item_return"><code>return</code></a> is found and if the last statement is an expression, its
value is returned. If the last statement is a loop control structure
like a <code>foreach</code> or a <code>while</code>, the returned value is unspecified. The
empty sub returns the empty list.</p>
<p>Perl does not have named formal parameters. In practice all you
do is assign to a <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> list of these. Variables that aren't
declared to be private are global variables. For gory details
on creating private variables, see <a href="../../lib/Pod/perlfunc.html#item_my">Private Variables via my()</a>
and <a href="../../lib/Pod/perlfunc.html#item_local">Temporary Values via local()</a>. To create protected
environments for a set of functions in a separate package (and
probably a separate file), see <a href="../../lib/Pod/perlmod.html#packages">Packages in the perlmod manpage</a>.</p>
<p>Example:</p>
<pre>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?