📄 ch06_01.htm
字号:
<html><head><title>Subroutines (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Subroutines"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch05_10.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part2.htm">Part 2: The Gory Details</a></td><td align="right" valign="top" width="172"><a href="ch06_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 6. Subroutines</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch06_01.htm">Syntax</a><br><a href="ch06_02.htm">Semantics</a><br><a href="ch06_03.htm">Passing References</a><br><a href="ch06_04.htm">Prototypes</a><br><a href="ch06_05.htm">Subroutine Attributes</a><br></p></div><a name="INDEX-1782"></a><a name="INDEX-1783"></a><a name="INDEX-1784"></a><a name="INDEX-1785"></a><a name="INDEX-1786"></a><a name="INDEX-1787"></a><p>Like many languages, Perl provides for user-definedsubroutines.<a href="#FOOTNOTE-1">[1]</a> These subroutines may be definedanywhere in the main program, loaded in from other files via the<tt class="literal">do</tt>, <tt class="literal">require</tt>, or<tt class="literal">use</tt> keywords, or generated at run time using<tt class="literal">eval</tt>. You can even load them at run time with themechanism described in the section <a href="ch10_02.htm#ch10-sect-auto">Section 6.2, "Autoloading"</a> in <a href="ch10_01.htm">Chapter 10, "Packages"</a>. You can call a subroutine indirectly,using a variable containing either its name or a reference to theroutine, or through an object, letting the object determine whichsubroutine should really be called. You can generate anonymoussubroutines, accessible only through references, and if you want, usethese to clone new, nearly identical functions via<em class="emphasis">closures</em>, which are covered in the section bythat name in <a href="ch08_01.htm">Chapter 8, "References"</a>.<a name="INDEX-1788"></a></p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1] We'll also call them<em class="emphasis">functions</em>, but functions are the same thing assubroutines in Perl. Sometimes we'll even call them<em class="emphasis">methods</em>, which are defined the same way, butcalled differently.</p></blockquote><h2 class="sect1">6.1. Syntax</h2><a name="INDEX-1789"></a><a name="INDEX-1790"></a><p></p><p>To declare a named subroutine without defining it, use one of these forms:<blockquote><pre class="programlisting">sub <em class="replaceable">NAME</em>sub <em class="replaceable">NAME</em> <em class="replaceable">PROTO</em>sub <em class="replaceable">NAME</em> <em class="replaceable">ATTRS</em>sub <em class="replaceable">NAME</em> <em class="replaceable">PROTO</em> <em class="replaceable">ATTRS</em></pre></blockquote>To declare and define a named subroutine, add a <em class="replaceable">BLOCK</em>:<a name="INDEX-1791"></a><blockquote><pre class="programlisting">sub <em class="replaceable">NAME</em> <em class="replaceable">BLOCK</em>sub <em class="replaceable">NAME</em> <em class="replaceable">PROTO</em> <em class="replaceable">BLOCK</em>sub <em class="replaceable">NAME</em> <em class="replaceable">ATTRS</em> <em class="replaceable">BLOCK</em>sub <em class="replaceable">NAME</em> <em class="replaceable">PROTO</em> <em class="replaceable">ATTRS</em> <em class="replaceable">BLOCK</em></pre></blockquote><a name="INDEX-1792"></a>To create an anonymous subroutine or closure, leave out the <em class="replaceable">NAME</em>:<blockquote><pre class="programlisting">sub <em class="replaceable">BLOCK</em>sub <em class="replaceable">PROTO</em> <em class="replaceable">BLOCK</em>sub <em class="replaceable">ATTRS</em> <em class="replaceable">BLOCK</em>sub <em class="replaceable">PROTO</em> <em class="replaceable">ATTRS</em> <em class="replaceable">BLOCK</em></pre></blockquote><em class="replaceable">PROTO</em> and <em class="replaceable">ATTRS</em>stand for the prototype and attributes, each of which is discussed inits own section later in the chapter. They're not so important--the<em class="replaceable">NAME</em> and the<em class="replaceable">BLOCK</em> are the essential parts, even whenthey're missing.</p><p><a name="INDEX-1793"></a><a name="INDEX-1794"></a>For the forms without a <em class="replaceable">NAME</em>, you still have to provide some way ofcalling the subroutine. So be sure to save the return value sincethis form of <tt class="literal">sub</tt> declaration is not only compiled at compile timeas you would expect, but also produces a run-time return value:<blockquote><pre class="programlisting">$subref = sub <em class="replaceable">BLOCK</em>;</pre></blockquote><a name="INDEX-1795"></a><a name="INDEX-1796"></a>To import subroutines defined in another module, say:<blockquote><pre class="programlisting">use <em class="replaceable">MODULE</em> qw(<em class="replaceable">NAME1</em> <em class="replaceable">NAME2</em> <em class="replaceable">NAME3</em>...);</pre></blockquote>To call subroutines directly, say:<blockquote><pre class="programlisting"><em class="replaceable">NAME</em>(<em class="replaceable">LIST</em>) # & is optional with parentheses.<em class="replaceable">NAME</em> <em class="replaceable">LIST</em> # Parens optional if sub predeclared/imported.&<em class="replaceable">NAME</em> # Exposes current @_ to that subroutine, # (and circumvents prototypes).</pre></blockquote><a name="INDEX-1797"></a><a name="INDEX-1798"></a>To call subroutines indirectly (by name or by reference), use any of these:<blockquote><pre class="programlisting">&$subref(<em class="replaceable">LIST</em>) # The & is not optional on indirect call$subref->(<em class="replaceable">LIST</em>) # (unless using infix notation).&$subref # Exposes current @_ to that subroutine.</pre></blockquote><a name="INDEX-1799"></a></p><p><a name="INDEX-1800"></a>The official name of a subroutine includes the<tt class="literal">&</tt> prefix. A subroutine may be called using theprefix, but the <tt class="literal">&</tt> is usually optional, and soare the parentheses if the subroutine has been predeclared. However,the <tt class="literal">&</tt> is not optional when you're just namingthe subroutine, such as when it's used as an argument to<tt class="literal">defined</tt> or <tt class="literal">undef</tt> or when youwant to generate a reference to a named subroutine by saying<tt class="literal">$subref = \&name</tt>. Nor is the<tt class="literal">&</tt> optional when you want to make an indirectsubroutine call using the <tt class="literal">&$subref()</tt> or<tt class="literal">&{$subref}()</tt> constructs. However, the moreconvenient <tt class="literal">$subref->()</tt> notation does not requireit. See <a href="ch08_01.htm">Chapter 8, "References"</a> for more aboutreferences to subroutines.</p><p><a name="INDEX-1801"></a><a name="INDEX-1802"></a>Perl doesn't force a particular capitalization style on yoursubroutine names. However, one loosely held convention is thatfunctions called indirectly by Perl's run-time system(<tt class="literal">BEGIN</tt>, <tt class="literal">CHECK</tt>,<tt class="literal">INIT</tt>, <tt class="literal">END</tt>,<tt class="literal">AUTOLOAD</tt>, <tt class="literal">DESTROY</tt>, and all thefunctions mentioned in <a href="ch14_01.htm">Chapter 14, "Tied Variables"</a>) arein all capitals, so you might want to avoid using that style. (Butsubroutines used for constant values are customarily named with allcaps too. That's okay. We hope...)</p><a name="INDEX-1883"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch05_10.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch06_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">5.10. Fancy Patterns</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">6.2. Semantics</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -