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

📄 ch04_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Subroutines (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="ch03_10.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>




<h1 class="chapter">Chapter 4. Subroutines</h1>
<div class="htmltoc"><h4 class="tochead">Contents:</h4>
  <p> <a href="#lperl3-CHP-4-SECT-1">System and User Functions</a><br />
<a href="ch04_02.htm">Defining a Subroutine</a><br />
<a href="ch04_03.htm">Invoking a Subroutine</a><br />
<a href="ch04_04.htm">Return Values</a><br />
<a href="ch04_05.htm">Arguments</a><br />
<a href="ch04_06.htm">Private Variables in Subroutines</a><br />
<a href="ch04_07.htm">The local Operator</a><br />
<a href="ch04_08.htm">Variable-length Parameter Lists</a><br />
<a href="ch04_09.htm">Notes on Lexical (my) Variables</a><br />
<a href="ch04_10.htm">The use strict Pragma</a><br />
<a href="ch04_11.htm">The return Operator</a><br />
<a href="ch04_12.htm">Exercises</a><br /></p></div>

<div class="sect1"><a name="lperl3-CHP-4-SECT-1" /></a>
<h2 class="sect1">4.1. System and User Functions</h2>

<p><a name="INDEX-313" /></a>We've
already seen and used some of the builtin <a name="INDEX-314" /></a>system functions, such as
<tt class="literal">chomp</tt>, <tt class="literal">reverse</tt>,
<tt class="literal">print</tt>, and so on. But, as other languages do, Perl
has the ability to make <em class="firstterm">subroutines</em>, which are
<a name="INDEX-315" /></a>user-defined
functions.<a href="#FOOTNOTE-97">[97]</a> These
let us recycle one chunk of code many times in one program.<a href="#FOOTNOTE-98">[98]</a>
</p><blockquote class="footnote"> <a name="FOOTNOTE-97" /></a><p>[97]In Perl, we don't generally make the
distinction that Pascal programmers are used to, between
<em class="emphasis">functions</em>, which return a value, and
<em class="emphasis">procedures</em>, which don't. But a
<em class="emphasis">subroutine</em> is always user-defined, while a
<em class="emphasis">function</em> may or may not be. That is, the word
<em class="emphasis">function</em> may be used as a synonym for
<em class="emphasis">subroutine</em>, or it may mean one of Perl's
builtin functions. That's why this chapter is titled
<em class="emphasis">Subroutines</em>, because it's about the ones
you can define, not the builtins. Mostly.</p> </blockquote><blockquote class="footnote">
<a name="FOOTNOTE-98" /></a><p>[98]The code examples used in this book are recycled from at least
40% post-consumer programming, and are at least 75% recyclable into
your programs when properly decomposed. </p> </blockquote>

<p>The name of a subroutine is another Perl identifier (letters, digits,
and underscores, but can't start with a digit) with a
sometimes-optional ampersand (<tt class="literal">&amp;</tt>) in front.
There's a rule about when you can omit the ampersand and when
you cannot; we'll see that rule by the end of the chapter. For
now, we'll just use it every time that it's not
forbidden, which is always a safe rule. And we'll tell you
every place where it's forbidden, of course.
</p>

<p>That subroutine name comes from a separate namespace, so Perl
won't be confused if you have a subroutine called
<tt class="literal">&amp;fred</tt> and a scalar called
<tt class="literal">$fred</tt> in the same program -- although
there's no reason to do that under normal circumstances.
</p>

</div>
























<hr width="684" align="left" />
<div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_10.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3.10. Exercises</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.2. Defining 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 + -