📄 ch10_17.htm
字号:
<HTML><HEAD><TITLE>Recipe 10.16. Nesting Subroutines (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:40:08Z"><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="ch10_01.htm"TITLE="10. Subroutines"><LINKREL="prev"HREF="ch10_16.htm"TITLE="10.15. Trapping Undefined Function Calls with AUTOLOAD"><LINKREL="next"HREF="ch10_18.htm"TITLE="10.17. Program: Sorting Your Mail"></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="ch10_16.htm"TITLE="10.15. Trapping Undefined Function Calls with AUTOLOAD"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.15. Trapping Undefined Function Calls with AUTOLOAD"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch10_01.htm"TITLE="10. Subroutines"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_18.htm"TITLE="10.17. Program: Sorting Your Mail"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.17. Program: Sorting Your Mail"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch10-22364">10.16. Nesting Subroutines</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1218">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch10-idx-1000004751-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004751-1"></A>You want to have nested subroutines, such that one subroutine is only visible and callable from another. When you try the obvious approach of nesting <CODECLASS="literal">sub</CODE> <CODECLASS="literal">FOO</CODE> <CODECLASS="literal">{</CODE> <CODECLASS="literal">sub</CODE> <CODECLASS="literal">BAR</CODE> <CODECLASS="literal">{</CODE> <CODECLASS="literal">}</CODE> <CODECLASS="literal">...</CODE> <CODECLASS="literal">}</CODE> Perl gives you warnings about variables that will not stay shared.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1224">Solution</A></H3><PCLASS="para">Instead of having the inner functions be normal subroutines, make them closures and temporarily assign them to the typeglob of the right name to create a localized function.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1230">Discussion</A></H3><PCLASS="para">If you use nested subroutines in other programming languages with their own private variables, you'll have to work at it a bit in Perl. The intuitive coding of this kind of thing gives the warning "will not stay shared". For example, this won't work:</P><PRECLASS="programlisting">sub outer { my $x = $_[0] + 35; sub inner { return $x * 19 } # WRONG return $x + inner();} </PRE><PCLASS="para">The following is a workaround:</P><PRECLASS="programlisting">sub outer { my $x = $_[0] + 35; local *inner = sub { return $x * 19 }; return $x + inner();} </PRE><PCLASS="para">Now <CODECLASS="literal">inner()</CODE> can only be called from within <CODECLASS="literal">outer()</CODE> because of the temporary assignments of the <ACLASS="indexterm"NAME="ch10-idx-1000006374-0"></A>closure. But when it does, it has normal access to the lexical variable <CODECLASS="literal">$x</CODE> from the scope of <CODECLASS="literal">outer()</CODE>.</P><PCLASS="para">This essentially creates a function local to another function, something not directly supported in Perl; however, the programming isn't always clear. <ACLASS="indexterm"NAME="ch10-idx-1000004753-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004753-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1000004401">See Also</A></H3><PCLASS="para">The sections on <ACLASS="olink"HREF="../prog/ch05_01.htm#PERL2-CH-5-SECT-1.1">"Symbol Tables"</A> in <ACLASS="olink"HREF="../prog/ch05_01.htm">Chapter 5</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlmod </I>(1); the section on <ACLASS="olink"HREF="../prog/ch04_03.htm#PERL2-CH-4-SECT-3.6">"Closures"</A> in <ACLASS="olink"HREF="../prog/ch04_01.htm">Chapter 4</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and the discussion of closures in <ICLASS="filename">perlref </I>(1); <ACLASS="xref"HREF="ch10_14.htm"TITLE="Saving Global Values">Recipe 10.13</A>; <ACLASS="xref"HREF="ch11_05.htm"TITLE="Taking References to Functions">Recipe 11.4</A></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="ch10_16.htm"TITLE="10.15. Trapping Undefined Function Calls with AUTOLOAD"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.15. Trapping Undefined Function Calls with AUTOLOAD"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_18.htm"TITLE="10.17. Program: Sorting Your Mail"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.17. Program: Sorting Your Mail"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">10.15. Trapping Undefined Function Calls with AUTOLOAD</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.17. Program: Sorting Your Mail</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 © 2002</a> O'Reilly & 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 + -