⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch04_02.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Defining a Subroutine (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch04_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">4.2. Defining a Subroutine</h2><p>To define your own<a name="INDEX-316" />subroutine,use the keyword <tt class="literal">sub</tt><a name="INDEX-317" />, the name of the subroutine (withoutthe ampersand), then the indented<a href="#FOOTNOTE-99">[99]</a> block of code (in curly braces) which makes up the<em class="firstterm">body</em><a name="INDEX-318" /> of the subroutine, something likethis:</p><blockquote class="footnote"> <a name="FOOTNOTE-99" /><p>[99]Okay, purists, weadmit it: the curly braces are part of the block, properly speaking.And Perl doesn't require the indentation of the block -- butyour maintenance programmer will. So please be stylish.</p></blockquote><blockquote><pre class="code">sub marine {  $n += 1;  # Global variable $n  print "Hello, sailor number $n!\n";}</pre></blockquote><p>Subroutine definitions can be anywhere in your program text, butprogrammers who come from a background of languages like C or Pascallike to put them at the start of the file. Others may prefer to putthem at the end of the file, so that the main part of the programappears at the beginning. It's up to you. In any case, youdon't normally need any kind of forward declaration.<a href="#FOOTNOTE-100">[100]</a></p><blockquote class="footnote"><a name="FOOTNOTE-100" /><p>[100]Unless your subroutine is being particularly tricky anddeclares a "prototype," which dictates how a compilerwill parse and interpret its invocation arguments. This israre -- see the <em class="emphasis">perlsub</em>manpage for more information.</p> </blockquote><p>Subroutine definitions are global; without some powerful trickiness,there are no private subroutines.<a href="#FOOTNOTE-101">[101]</a> If you have twosubroutine definitions with the same name, the later one overwritesthe earlier one.<a href="#FOOTNOTE-102">[102]</a> That's generally considered bad form, or the signof a confused maintenance programmer.</p><blockquote class="footnote"> <a name="FOOTNOTE-101" /><p>[101]If you wish to bepowerfully tricky, read the Perl documentation about coderefs storedin private (lexical) variables. </p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-102" /><p>[102]A warnable offense, however.</p></blockquote><p>As you may have noticed in the previous example, you may use anyglobal variables within the subroutine body. In fact, all of thevariables we've seen so far are globals; that is, they areaccessible from every part of your program. This horrifies linguisticpurists, but the Perl development team formed an angry mob withtorches and ran them out of town years ago. We'll see how tomake private variables in the section "Private Variables inSubroutines" later in this chapter.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch04_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">4. Subroutines</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">4.3. Invoking a Subroutine</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

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