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

📄 ch10_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:39:26Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch09_13.htm"TITLE="9.12. Program: lst"><LINKREL="next"HREF="ch10_02.htm"TITLE="10.1. Accessing Subroutine Arguments"></HEAD><BODYBGCOLOR="#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="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_13.htm"TITLE="9.12. Program: lst"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.12. Program: lst"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_02.htm"TITLE="10.1. Accessing Subroutine Arguments"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.1. Accessing Subroutine Arguments"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch10-23479">10. Subroutines</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch10-25054"TITLE="10.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch10_02.htm"TITLE="10.1. Accessing Subroutine Arguments">Accessing Subroutine Arguments</A><BR><ACLASS="sect1"HREF="ch10_03.htm"TITLE="10.2. Making Variables Private to a Function">Making Variables Private to a Function</A><BR><ACLASS="sect1"HREF="ch10_04.htm"TITLE="10.3. Creating Persistent Private Variables">Creating Persistent Private Variables</A><BR><ACLASS="sect1"HREF="ch10_05.htm"TITLE="10.4. Determining Current Function Name">Determining Current Function Name</A><BR><ACLASS="sect1"HREF="ch10_06.htm"TITLE="10.5. Passing Arrays and Hashes by Reference">Passing Arrays and Hashes by Reference</A><BR><ACLASS="sect1"HREF="ch10_07.htm"TITLE="10.6. Detecting Return Context">Detecting Return Context</A><BR><ACLASS="sect1"HREF="ch10_08.htm"TITLE="10.7. Passing by Named Parameter">Passing by Named Parameter</A><BR><ACLASS="sect1"HREF="ch10_09.htm"TITLE="10.8. Skipping Selected Return Values">Skipping Selected Return Values</A><BR><ACLASS="sect1"HREF="ch10_10.htm"TITLE="10.9. Returning More Than One Array or Hash">Returning More Than One Array or Hash</A><BR><ACLASS="sect1"HREF="ch10_11.htm"TITLE="10.10. Returning Failure">Returning Failure</A><BR><ACLASS="sect1"HREF="ch10_12.htm"TITLE="10.11. Prototyping Functions">Prototyping Functions</A><BR><ACLASS="sect1"HREF="ch10_13.htm"TITLE="10.12. Handling Exceptions">Handling Exceptions</A><BR><ACLASS="sect1"HREF="ch10_14.htm"TITLE="10.13. Saving Global Values">Saving Global Values</A><BR><ACLASS="sect1"HREF="ch10_15.htm"TITLE="10.14. Redefining a Function">Redefining a Function</A><BR><ACLASS="sect1"HREF="ch10_16.htm"TITLE="10.15. Trapping Undefined Function Calls with AUTOLOAD">Trapping Undefined Function Calls with AUTOLOAD</A><BR><ACLASS="sect1"HREF="ch10_17.htm"TITLE="10.16. Nesting Subroutines">Nesting Subroutines</A><BR><ACLASS="sect1"HREF="ch10_18.htm"TITLE="10.17. Program: Sorting Your Mail">Program: Sorting Your Mail</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>Composing mortals with immortal fire.</I></P><PCLASS="attribution"ALIGN="right">-&nbsp;W. H. Auden <CITECLASS="citetitle">"Three Songs for St Cecilia's Day" </CITE></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch10-25054">10.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch10-idx-1000004628-0"></A>To avoid the dangerous practice of copying and pasting code throughout a program, your larger programs will probably reuse chunks of code with subroutines. We'll use the terms <ICLASS="firstterm">subroutine</I> and <ICLASS="firstterm">function</I><ACLASS="indexterm"NAME="ch10-idx-1000004630-0"></A> interchangeably, because Perl doesn't distinguish between the two any more than C does. Even object-oriented methods are just subroutines that are called using a special syntax, described in <ACLASS="xref"HREF="ch13_01.htm"TITLE="Classes, Objects, and Ties">Chapter 13, <CITECLASS="chapter">Classes, Objects, and Ties</CITE></A>.</P><PCLASS="para">A subroutine is declared with the <CODECLASS="literal">sub</CODE><ACLASS="indexterm"NAME="ch10-idx-1000004631-0"></A> keyword. Here's a simple subroutine definition:</P><PRECLASS="programlisting">sub hello {     $greeted++;          # global variable     print &quot;hi there!\n&quot;;}</PRE><PCLASS="para">The typical way of calling that subroutine is:</P><PRECLASS="programlisting">hello();                 # call subroutine hello with no arguments/parameters</PRE><PCLASS="para">Because Perl compiles your program before executing it, it doesn't matter where your subroutines are declared. These definitions don't have to be in the same file as your main program. They can be pulled in from other files using the <CODECLASS="literal">do</CODE>, <CODECLASS="literal">require</CODE>, or <CODECLASS="literal">use</CODE> operators, as described in <ACLASS="xref"HREF="ch12_01.htm"TITLE="Packages, Libraries, and Modules">Chapter 12, <CITECLASS="chapter">Packages, Libraries, and Modules</CITE></A>. They can even be created on the fly using <CODECLASS="literal">eval</CODE> or the AUTOLOAD mechanism, or generated using closures, which can be used as function templates.</P><PCLASS="para">If you are familiar with other programming languages, several characteristics of Perl's functions may surprise you if you're not prepared. Most of the recipes in this chapter illustrate how to take advantage of &nbsp;-  and be aware of &nbsp;-  these properties.</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch10-pgfId-54"></A>Perl functions have no formal, named parameters, but this is not necessarily a bad thing. See Recipes <ACLASS="xref"HREF="ch10_02.htm"TITLE="Accessing Subroutine Arguments">10.1</A> and <ACLASS="xref"HREF="ch10_08.htm"TITLE="Passing by Named Parameter">10.7</A>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch10-pgfId-56"></A>All variables are global unless declared otherwise. See Recipes <ACLASS="xref"HREF="ch10_03.htm"TITLE="Making Variables Private to a Function">10.2</A>, <ACLASS="xref"HREF="ch10_04.htm"TITLE="Creating Persistent Private Variables">10.3</A>, and <ACLASS="xref"HREF="ch10_14.htm"TITLE="Saving Global Values">10.13</A> for details.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch10-pgfId-58"></A>Passing or returning more than one array or hash normally causes them to lose their separate identities. See Recipes <ACLASS="xref"HREF="ch10_06.htm"TITLE="Passing Arrays and Hashes by Reference">10.5</A>, <ACLASS="xref"HREF="ch10_09.htm"TITLE="Skipping Selected Return Values">10.8</A>, <ACLASS="xref"HREF="ch10_10.htm"TITLE="Returning More Than One Array or Hash">10.9</A>, and <ACLASS="xref"HREF="ch10_12.htm"TITLE="Prototyping Functions">10.11</A> to avoid this.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch10-pgfId-60"></A>A function can know whether it was called in list or scalar context, how many arguments it was called with, and even the name of the function that called it. See Recipes <ACLASS="xref"HREF="ch10_05.htm"TITLE="Determining Current Function Name">10.4</A> and <ACLASS="xref"HREF="ch10_07.htm"TITLE="Detecting Return Context">10.6</A> to find out how.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch10-pgfId-62"></A>Perl's <CODECLASS="literal">undef</CODE> value can be used to indicate an error condition since no valid string or number ever has that value. <ACLASS="xref"HREF="ch10_11.htm"TITLE="Returning Failure">10.10</A> covers subtle pitfalls with <CODECLASS="literal">undef</CODE> you should avoid, and <ACLASS="xref"HREF="ch10_13.htm"TITLE="Handling Exceptions">10.12</A> shows how to deal with other catastrophic conditions.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch10-pgfId-64"></A>Perl supports interesting operations on functions you might not see in other languages, like anonymous functions, creating functions on the fly, and calling them indirectly using function pointers. See Recipes <ACLASS="xref"HREF="ch10_15.htm"TITLE="Redefining a Function">10.14</A> and <ACLASS="xref"HREF="ch10_17.htm"TITLE="Nesting Subroutines">10.16</A> for these esoteric topics.</P></LI></UL><PCLASS="para">Calling a function as <CODECLASS="literal">$x</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">&amp;func;</CODE> does not supply any arguments, but rather provides direct access to its caller's <CODECLASS="literal">@_</CODE> array! If you omit the ampersand and use either <CODECLASS="literal">func()</CODE> or <CODECLASS="literal">func</CODE>, then a new and empty <CODECLASS="literal">@_</CODE> is provided instead.</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_13.htm"TITLE="9.12. Program: lst"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.12. Program: lst"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_02.htm"TITLE="10.1. Accessing Subroutine Arguments"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.1. Accessing Subroutine Arguments"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">9.12. Program: lst</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">10.1. Accessing Subroutine Arguments</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + -